我一直在嘗試查看項目的詳細信息,只要我點擊ListView
即可。但我不斷收到錯誤,說無法實例化活動。致命錯誤:無法激活活動組件
這裏是我的logcat
10-29 03:01:44.516: E/AndroidRuntime(1252): FATAL EXCEPTION: main
10-29 03:01:44.516: E/AndroidRuntime(1252): Process: com.esggoa.iffi_app, PID: 1252
10-29 03:01:44.516: E/AndroidRuntime(1252): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.esggoa.iffi_app/com.esggoa.iffi_app.model.SingleItemView}: java.lang.NullPointerException
10-29 03:01:44.516: E/AndroidRuntime(1252): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
10-29 03:01:44.516: E/AndroidRuntime(1252): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-29 03:01:44.516: E/AndroidRuntime(1252): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-29 03:01:44.516: E/AndroidRuntime(1252): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-29 03:01:44.516: E/AndroidRuntime(1252): at android.os.Handler.dispatchMessage(Handler.java:102)
10-29 03:01:44.516: E/AndroidRuntime(1252): at android.os.Looper.loop(Looper.java:136)
10-29 03:01:44.516: E/AndroidRuntime(1252): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-29 03:01:44.516: E/AndroidRuntime(1252): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 03:01:44.516: E/AndroidRuntime(1252): at java.lang.reflect.Method.invoke(Method.java:515)
10-29 03:01:44.516: E/AndroidRuntime(1252): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-29 03:01:44.516: E/AndroidRuntime(1252): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-29 03:01:44.516: E/AndroidRuntime(1252): at dalvik.system.NativeStart.main(Native Method)
10-29 03:01:44.516: E/AndroidRuntime(1252): Caused by: java.lang.NullPointerException
10-29 03:01:44.516: E/AndroidRuntime(1252): at com.esggoa.iffi_app.model.SingleItemView.<init>(SingleItemView.java:26)
10-29 03:01:44.516: E/AndroidRuntime(1252): at java.lang.Class.newInstanceImpl(Native Method)
10-29 03:01:44.516: E/AndroidRuntime(1252): at java.lang.Class.newInstance(Class.java:1208)
10-29 03:01:44.516: E/AndroidRuntime(1252): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-29 03:01:44.516: E/AndroidRuntime(1252): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
10-29 03:01:44.516: E/AndroidRuntime(1252): ... 11 more
而且因爲它規定
Caused by: java.lang.NullPointerException
10-29 03:01:44.516: E/AndroidRuntime(1252): at com.esggoa.iffi_app.model.SingleItemView.<init>(SingleItemView.java:26)
我張貼下面
public class SingleItemView extends Fragment {
// Declare Variables
String rank;
String country;
String population;
String flag;
String position;
ImageLoader imageLoader = new ImageLoader(getActivity().getApplicationContext());
private FragmentActivity fa;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.singleitemview, container, false);
return rootView;
}
@Override
public void onStart() {
super.onStart();
// Get the view from singleitemview.xml
Intent i = new Intent(fa, SingleItemView.class);
// Get the result of rank
rank = i.getStringExtra("rank");
// Get the result of country
country = i.getStringExtra("country");
// Get the result of population
population = i.getStringExtra("population");
// Get the result of flag
flag = i.getStringExtra("flag");
// Locate the TextViews in singleitemview.xml
TextView txtrank = (TextView) getView().findViewById(R.id.rank);
TextView txtcountry = (TextView) getView().findViewById(R.id.country);
TextView txtpopulation = (TextView) getView().findViewById(R.id.population);
// Locate the ImageView in singleitemview.xml
ImageView imgflag = (ImageView) getView().findViewById(R.id.flag);
// Set results to the TextViews
txtrank.setText(rank);
txtcountry.setText(country);
txtpopulation.setText(population);
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(flag, imgflag);
}
}
我SingleItemView.java任何幫助表示讚賞
10-29 05:20:12.075: E/AndroidRuntime(1108): FATAL EXCEPTION: main
10-29 05:20:12.075: E/AndroidRuntime(1108): Process: com.esggoa.iffi_app, PID: 1108
10-29 05:20:12.075: E/AndroidRuntime(1108): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.esggoa.iffi_app/com.esggoa.iffi_app.model.SingleItemView}: java.lang.ClassCastException: com.esggoa.iffi_app.model.SingleItemView cannot be cast to android.app.Activity
10-29 05:20:12.075: E/AndroidRuntime(1108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
10-29 05:20:12.075: E/AndroidRuntime(1108): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-29 05:20:12.075: E/AndroidRuntime(1108): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-29 05:20:12.075: E/AndroidRuntime(1108): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-29 05:20:12.075: E/AndroidRuntime(1108): at android.os.Handler.dispatchMessage(Handler.java:102)
10-29 05:20:12.075: E/AndroidRuntime(1108): at android.os.Looper.loop(Looper.java:136)
10-29 05:20:12.075: E/AndroidRuntime(1108): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-29 05:20:12.075: E/AndroidRuntime(1108): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 05:20:12.075: E/AndroidRuntime(1108): at java.lang.reflect.Method.invoke(Method.java:515)
10-29 05:20:12.075: E/AndroidRuntime(1108): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-29 05:20:12.075: E/AndroidRuntime(1108): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-29 05:20:12.075: E/AndroidRuntime(1108): at dalvik.system.NativeStart.main(Native Method)
10-29 05:20:12.075: E/AndroidRuntime(1108): Caused by: java.lang.ClassCastException: com.esggoa.iffi_app.model.SingleItemView cannot be cast to android.app.Activity
10-29 05:20:12.075: E/AndroidRuntime(1108): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-29 05:20:12.075: E/AndroidRuntime(1108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
10-29 05:20:12.075: E/AndroidRuntime(1108): ... 11 more
可否請你試着去了解和調試代碼,作爲開發人員,你會遇到數百這些情況。 – meh 2014-10-29 07:14:55
@meh我是非常新的android和我不知道調試技術 – 2014-10-29 08:03:28
調試你的代碼是最好的學習方式 – meh 2014-10-29 08:14:14