2014-10-29 127 views
-3

我一直在嘗試查看項目的詳細信息,只要我點擊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 
+1

可否請你試着去了解和調試代碼,作爲開發人員,你會遇到數百這些情況。 – meh 2014-10-29 07:14:55

+0

@meh我是非常新的android和我不知道調試技術 – 2014-10-29 08:03:28

+0

調試你的代碼是最好的學習方式 – meh 2014-10-29 08:14:14

回答

0

移動在onCreateView(...)所有views'的所有初始化,並宣佈在一流水平的意見globally。從意圖獲取信息是錯誤的。像這樣做

public class SingleItemView extends Fragment { 
    // Declare Variables 
    private String rank; 
    private String country; 
    private String population; 
    private String flag; 
    private String position; 
    private TextView txtrank; 
    private TextView txtcountry; 
    private TextView txtpopulation; 
    private ImageView imgflag; 
    ImageLoader imageLoader; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) 
    { 

     View rootView = inflater.inflate(R.layout.singleitemview, container, false); 
     rank = getArguments().getString("rank"); 
     country = getArguments().getString("country"); 
     population = getArguments().getString("population"); 
     flag = getArguments().getString("flag"); 

     txtrank = (TextView) rootView.findViewById(R.id.rank); 
     txtcountry = (TextView) rootView.findViewById(R.id.country); 
     txtpopulation = (TextView) rootView.findViewById(R.id.population); 
     imgflag = (ImageView) rootView.findViewById(R.id.flag); 

     txtrank.setText(rank); 
     txtcountry.setText(country); 
     txtpopulation.setText(population); 

     imageLoader = new ImageLoader(getActivity()); 
     // Capture position and set results to the ImageView 
     // Passes flag images URL into ImageLoader.class 
     imageLoader.DisplayImage(flag, imgflag); 

     return rootView; 
    } 
} 
+0

非常感謝答覆,我改變了我的代碼,你已經發送了,幸運的是我離開了NullPointerException但現在ClassCastException發生說SingleItemView不能轉換爲android.app.activity – 2014-10-29 08:02:32

+0

@saishneugi:'SingleItemView'不是* Activity *它是* Fragment *後您最近的logcat錯誤。 – kId 2014-10-29 08:39:34

+0

請檢查我最近登錄的logcat – 2014-10-29 09:34:05

0

您不能在View的開始處使用FragmentContext。就在這條線

ImageLoader imageLoader = new ImageLoader(getActivity().getApplicationContext()); 

改變

ImageLoader imageLoader; 

onCreateView()方法初始化ImageLoader類。

imageLoader = new ImageLoader(getActivity()); 

而且SingleItemView是你Fragment類,因此無法通過Intent調用它。

Intent i = new Intent(fa, SingleItemView.class); 
0

onCreateView():調用來創建與片段關聯的視圖層次結構。 您最好使用onActivityCreated()而不是onStart()。

private TextView txtrank; 
private TextView txtcountry; 
private TextView txtpopulation; 
private ImageView imgflag; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) 
{ 

    View rootView = inflater.inflate(R.layout.singleitemview, container, false); 
    // Locate the TextViews in singleitemview.xml 
    txtrank = (TextView) rootView .findViewById(R.id.rank); 
    txtcountry = (TextView) rootView .findViewById(R.id.country); 
    txtpopulation = (TextView)rootView .findViewById(R.id.population); 

    // Locate the ImageView in singleitemview.xml 
    imgflag = (ImageView) rootView .findViewById(R.id.flag); 
    return rootView; 

} 
@Override 
public void onStart() { 
    //......bla bla 
    // 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); 

}