0

面临一个错误,上面写着My ListFragment Cannot be cast to android.app.Activity。它适用于具有 2 窗格布局的平板电脑应用程序,这是左侧。不知道为什么会崩溃。任何帮助都是极好的。

代码:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivityTab extends ListFragment {

FragmentManager fm;
FragmentTransaction ft;
Fragment myFragment;


@Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String[] values = new String[] { "Player", "", "About This",
        "The Science", "Information", "Instructions", "PDF"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);

    FragmentManager fm = getActivity().getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    if (savedInstanceState == null){
        Fragment_11 myFragment = new Fragment_11();
        ft.add(R.id.content_layout, myFragment);
        ft.commit(); }}


 @Override
 public void onListItemClick(ListView l, View v, int position, long id){
Fragment newFragment = null;
FragmentManager fm = getActivity().getSupportFragmentManager();
switch(position){
case 0:
    newFragment = new Fragment_11();
    break;
case 1:
    newFragment = new Fragment_2();
    break;
case 2:
    newFragment = new Fragment_3();
    break;
case 3:
    newFragment = new Fragment_4();
    break;
case 4:
    newFragment = new Fragment_5();
    break;
case 5:
    newFragment = new Fragment_6();
    break;

}
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_layout, newFragment);
ft.addToBackStack(null)
.commit();
 }

 }

错误:

 09-04 12:07:24.897: E/AndroidRuntime(32209): FATAL EXCEPTION: main
 09-04 12:07:24.897: E/AndroidRuntime(32209): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.appexample.sseconds/com.appexample.sseconds.MainActivityTab}: java.lang.ClassCastException: com.appexample.sseconds.MainActivityTab cannot be cast to  android.app.Activity
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at android.app.ActivityThread.access$600(ActivityThread.java:141)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at android.os.Handler.dispatchMessage(Handler.java:99)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at android.os.Looper.loop(Looper.java:137)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at android.app.ActivityThread.main(ActivityThread.java:5103)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at java.lang.reflect.Method.invokeNative(Native Method)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at java.lang.reflect.Method.invoke(Method.java:525)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at dalvik.system.NativeStart.main(Native Method)
 09-04 12:07:24.897: E/AndroidRuntime(32209): Caused by: java.lang.ClassCastException: com.appexample.sseconds.MainActivityTab cannot be cast to android.app.Activity
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
 09-04 12:07:24.897: E/AndroidRuntime(32209):   ... 11 more
4

1 回答 1

1

不知道崩溃在哪里,但你做的一切都是错误的。在 Fragment 代码中的 Activity 上启动 FragmentTransaction 始终是一种不好的做法。在扩展 Activity 类中执行所有事务操作。如果你需要 OnListItemClick 事件 - 你可以做

((MyActivity) getActivity()).onListItemClick()

交付事件。

于 2013-09-04T11:15:13.503 回答