2014-10-29 124 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); 

}