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 infl开发者_Go百科ater, 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?
Looking at the stack trace it seems like you are treating MainActivity
as an actual Activity
, which it is not, it is a Fragment
. It seems like you either don't fully understand Fragments
or something about the design of your application is flawed.
精彩评论