根据我之前的经验,我尝试使用BaseAdapter
列出一些自定义列表项,它使用array
values.xml中定义的string
中的两个字符串我的应用程序崩溃说我的片段没有附加活动。
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
答案 0 :(得分:9)
您必须考虑片段生命周期:onCreateView
在onActivityCreated
之前,这意味着活动可能不在那里,因此在您{{1}的适配器中为活动提供getActivity()
给你这个例外。
http://developer.android.com/guide/components/fragments.html