我的片段没有被附接到所述主要活动类。(My Fragment is not being attach

2019-10-20 04:53发布

以我以往的经验,我试图列出使用一些自定义列表项BaseAdapter ,它使用两个字符串array中定义string values.xml我的应用程序崩溃说我的片段不附有活动。

MainActivity.java是:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        MyListFragment myListFragment = new MyListFragment();
        fragmentTransaction.replace(android.R.id.content, myListFragment);
        fragmentTransaction.commit();

    }

}

MyListFragment.java是:

public class MyListFragment extends ListFragment{

    MyCustomAdapter mAdapter;
    String[] title = getResources().getStringArray(R.array.list_items);
    String[] details = getResources().getStringArray(R.array.sub_list_items);

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.list_view, container,false);

        mAdapter = new MyCustomAdapter(title, details, getActivity());

        ListView contListView = (ListView)rootView.findViewById(android.R.id.list);
        contListView.setAdapter(mAdapter);

        return rootView;
    }
}

MyCustomAdapter.java是:

public class MyCustomAdapter extends BaseAdapter {

    private Context context;
    String[] Title;
    String[] Detail;

    public MyCustomAdapter(String[] text, String[] text1,Context context) {
        Title = text;
        Detail = text1;
        this.context = context;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Title.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    // variable to hold context


    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row;
        row = inflater.inflate(R.layout.fragment_main, parent, false);
        TextView title, detail;
        title = (TextView) row.findViewById(R.id.textView1);
        detail = (TextView) row.findViewById(R.id.textView2);
        title.setText(Title[position]);
        detail.setText(Detail[position]);

        return (row);
    }
}

logcat的报告后,崩溃的是:

05-25 16:18:46.481: E/AndroidRuntime(23629): FATAL EXCEPTION: main
05-25 16:18:46.481: E/AndroidRuntime(23629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.listeventhandling/com.android.listeventhandling.MainActivity}: java.lang.IllegalStateException: Fragment MyListFragment{40d6b5e8} not attached to Activity
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.os.Looper.loop(Looper.java:154)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.app.ActivityThread.main(ActivityThread.java:4945)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at java.lang.reflect.Method.invokeNative(Native Method)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at java.lang.reflect.Method.invoke(Method.java:511)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at dalvik.system.NativeStart.main(Native Method)
05-25 16:18:46.481: E/AndroidRuntime(23629): Caused by: java.lang.IllegalStateException: Fragment MyListFragment{40d6b5e8} not attached to Activity
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.app.Fragment.getResources(Fragment.java:817)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at com.android.listeventhandling.MyListFragment.<init>(MyListFragment.java:12)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at com.android.listeventhandling.MainActivity.onCreate(MainActivity.java:18)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.app.Activity.performCreate(Activity.java:4531)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
05-25 16:18:46.481: E/AndroidRuntime(23629):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
05-25 16:18:46.481: E/AndroidRuntime(23629):    ... 11 more

Answer 1:

你要介意片段的生命周期: onCreateView是前onActivityCreated这意味着活动可能不会出现,因此提供与活动getActivity()在你的适配器onCreateView给你这个例外。

http://developer.android.com/guide/components/fragments.html



文章来源: My Fragment is not being attached to the Main Activity Class.