我有一个应该填充列表视图的类。我使用相同的代码到另一个活动(不是片段),它工作正常。相反,在这个片段中,它给了我一个“IllegalStateException”......
这是日志:
04-02 14:08:21.505: E/AndroidRuntime(25925): FATAL EXCEPTION: main
04-02 14:08:21.505: E/AndroidRuntime(25925): Process: com.social.bearv2, PID: 25925
04-02 14:08:21.505: E/AndroidRuntime(25925): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.social.bearv2/com.social.bearv2.PagerTabsActivity}: java.lang.IllegalStateException: Content view not yet created
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.app.ActivityThread.access$800(ActivityThread.java:145)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.os.Handler.dispatchMessage(Handler.java:102)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.os.Looper.loop(Looper.java:136)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.app.ActivityThread.main(ActivityThread.java:5081)
04-02 14:08:21.505: E/AndroidRuntime(25925): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 14:08:21.505: E/AndroidRuntime(25925): at java.lang.reflect.Method.invoke(Method.java:515)
04-02 14:08:21.505: E/AndroidRuntime(25925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-02 14:08:21.505: E/AndroidRuntime(25925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-02 14:08:21.505: E/AndroidRuntime(25925): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
04-02 14:08:21.505: E/AndroidRuntime(25925): at dalvik.system.NativeStart.main(Native Method)
04-02 14:08:21.505: E/AndroidRuntime(25925): Caused by: java.lang.IllegalStateException: Content view not yet created
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
04-02 14:08:21.505: E/AndroidRuntime(25925): at com.social.bearv2.Page2Fragment.<init>(Page2Fragment.java:46)
04-02 14:08:21.505: E/AndroidRuntime(25925): at java.lang.Class.newInstanceImpl(Native Method)
04-02 14:08:21.505: E/AndroidRuntime(25925): at java.lang.Class.newInstance(Class.java:1208)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
04-02 14:08:21.505: E/AndroidRuntime(25925): at com.social.bearv2.PagerTabsActivity.onCreate(PagerTabsActivity.java:36)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.app.Activity.performCreate(Activity.java:5231)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-02 14:08:21.505: E/AndroidRuntime(25925): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-02 14:08:21.505: E/AndroidRuntime(25925): ... 12 more
这是班级:
public class Page2Fragment extends ListFragment {
List<NameValuePair> nameValuePairs;
ArrayList<String> items = null;
HttpClient httpclient;
HttpPost httppost;
HttpResponse response;
HttpEntity httpentity;
String is;
private static SharedPreferences pref;
private TextView testo;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Se container è vuoto restituisco null
if (container == null) {
return null;
}
// Altrimenti faccio l'inflate della view dal layout
View view = (LinearLayout) inflater.inflate(R.layout.page2, container,
false);
Context ctx = getActivity();
Resources res = ctx.getResources();
String[] achievements = getResources().getStringArray(R.array.achievements);
TypedArray immagini = res.obtainTypedArray(R.array.immagini);
setListAdapter(new TestImmagineAdapter(ctx, R.layout.riga_lista, achievements, immagini));
//setListAdapter(new ArrayAdapter<String>(this, R.layout.riga_lista, achievements));
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
testo = (TextView) view.findViewById(R.id.testo_vista);
pref = getActivity().getSharedPreferences("MainActivity", 0);
final SharedPreferences.Editor editor = pref.edit();
if(pref.getInt("ach1", 0)== 1) {
lv.getChildAt(0).setBackgroundColor(Color.WHITE);
}
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
//Toast.makeText(getApplicationContext(), testo.getText(),
//Toast.LENGTH_SHORT).show();
int num = position;
Log.i("Elemento numero:", Integer.toString(num));
}
});
return view;
}
}
答案 0 :(得分:1)
onCreateView
永远不应该返回null
。您应该使用onCreateView
只是为了返回夸大的View
并使用onViewCreated
来查找夸大视图的元素。 onViewCreated
的第一个参数是您膨胀的View
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.page2, container,
false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Context ctx = getActivity();
Resources res = ctx.getResources();
String[] achievements = getResources().getStringArray(R.array.achievements);
TypedArray immagini = res.obtainTypedArray(R.array.immagini);
setListAdapter(new TestImmagineAdapter(ctx, R.layout.riga_lista, achievements, immagini));
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
testo = (TextView) view.findViewById(R.id.testo_vista);
pref = getActivity().getSharedPreferences("MainActivity", 0);
final SharedPreferences.Editor editor = pref.edit();
if(pref.getInt("ach1", 0)== 1) {
lv.getChildAt(0).setBackgroundColor(Color.WHITE);
}
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
//Toast.makeText(getApplicationContext(), testo.getText(),
//Toast.LENGTH_SHORT).show();
int num = position;
Log.i("Elemento numero:", Integer.toString(num));
}
});
}
答案 1 :(得分:0)
我只是猜测,但有可能你没有打电话:
setContentView(...)
<{1}}的{{1}}方法中的?