使用 Fragments 自定义标题
我有一个使用自定义标题功能的应用程序。我正在尝试将其迁移到fragments api。我得到的结果代码如下:
public class MainActivity extends CategoriesListActivity {
protected static final String TAG = "MainActivity";
/** Called when the activity is first created. */
public void onActivityCreated(Bundle savedInstanceState) {
getActivity().requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onActivityCreated(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new MecomUncaughtExceptionHandler(getActivity()));
getActivity().setContentView(R.layout.main);
getActivity().getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main_title);
}
}
其中 CategoriesListActivity
扩展 ListFragment
。 main_title.xml
是这样的:
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.mecom.berlingske.fragments.HeaderFragment"
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
HeaderFragment
是:
public class HeaderFragment extends Fragment implements OnClickListener {
private static final String TAG = "Header";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_title_fragment, container, false);
}
}
而 main_title_fragment.xml
是:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<ImageView
android:id="@+id/berlingske_logo"
android:src="@drawable/main_title_bg"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClick"/>
<View
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"
android:visibility="invisible"/>
<Button
android:id="@+id/buttonTitleSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_menu_search"
android:layout_gravity="fill_vertical"
style="@style/BerlingskeMainTitleButton"
android:onClick="onClick"/>
<Button
android:id="@+id/buttnTitleFeedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_menu_light"
android:layout_gravity="fill_vertical"
style="@style/BerlingskeMainTitleButton"
android:onClick="onClick"/>
</LinearLayout>
当我运行这个时,它在此 Activity< 后崩溃/code> 膨胀了:
E/AndroidRuntime( 545): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mecom.berlingske/com.mecom.berlingske.MainActivity}: java.lang.ClassCastException: com.mecom.berlingske.MainActivity
E/AndroidRuntime( 545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime( 545): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime( 545): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime( 545): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime( 545): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 545): at android.os.Looper.loop(Looper.java:123)
E /AndroidRuntime( 545): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 545): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 545): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 545): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 545): Caused by: java.lang.ClassCastException: com.mecom.berlingske.MainActivity
E/AndroidRuntime( 545): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime( 545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
E/AndroidRuntime( 545): ... 11 more
我在这里没有看到任何转换问题。有人尝试将片段设置为自定义标题吗?
I've got an app, that uses custom title feature. I'm trying to migrate it to fragments api. Resulting code, I've got, is like this:
public class MainActivity extends CategoriesListActivity {
protected static final String TAG = "MainActivity";
/** Called when the activity is first created. */
public void onActivityCreated(Bundle savedInstanceState) {
getActivity().requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onActivityCreated(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new MecomUncaughtExceptionHandler(getActivity()));
getActivity().setContentView(R.layout.main);
getActivity().getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main_title);
}
}
Where CategoriesListActivity
extends ListFragment
. main_title.xml
is this:
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.mecom.berlingske.fragments.HeaderFragment"
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
HeaderFragment
is:
public class HeaderFragment extends Fragment implements OnClickListener {
private static final String TAG = "Header";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_title_fragment, container, false);
}
}
and main_title_fragment.xml
is:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<ImageView
android:id="@+id/berlingske_logo"
android:src="@drawable/main_title_bg"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClick"/>
<View
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"
android:visibility="invisible"/>
<Button
android:id="@+id/buttonTitleSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_menu_search"
android:layout_gravity="fill_vertical"
style="@style/BerlingskeMainTitleButton"
android:onClick="onClick"/>
<Button
android:id="@+id/buttnTitleFeedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_menu_light"
android:layout_gravity="fill_vertical"
style="@style/BerlingskeMainTitleButton"
android:onClick="onClick"/>
</LinearLayout>
When I run this, it crashes after this Activity
is inflated:
E/AndroidRuntime( 545): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mecom.berlingske/com.mecom.berlingske.MainActivity}: java.lang.ClassCastException: com.mecom.berlingske.MainActivity
E/AndroidRuntime( 545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime( 545): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime( 545): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime( 545): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime( 545): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 545): at android.os.Looper.loop(Looper.java:123)
E /AndroidRuntime( 545): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 545): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 545): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 545): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 545): Caused by: java.lang.ClassCastException: com.mecom.berlingske.MainActivity
E/AndroidRuntime( 545): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime( 545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
E/AndroidRuntime( 545): ... 11 more
I don't see any cast problems here. Anyone tried to set a fragment to be a custom title?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看堆栈跟踪,您似乎将
MainActivity
视为实际的Activity
,但事实并非如此,它是一个Fragment
。您似乎没有完全理解Fragments
或者您的应用程序的设计存在缺陷。Looking at the stack trace it seems like you are treating
MainActivity
as an actualActivity
, which it is not, it is aFragment
. It seems like you either don't fully understandFragments
or something about the design of your application is flawed.