每当我点击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)
我在
下面发布我的SingleItemView.javapublic 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);
}
}
感谢任何帮助
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
答案 0 :(得分:0)
您无法在View的开头使用Fragment
Context
。只需更改此行
ImageLoader imageLoader = new ImageLoader(getActivity().getApplicationContext());
到
ImageLoader imageLoader;
并在ImageLoader
方法中初始化您的onCreateView()
课程。
imageLoader = new ImageLoader(getActivity());
同样SingleItemView
是您的Fragment
课程,因此您无法通过Intent
进行调用。
Intent i = new Intent(fa, SingleItemView.class);
答案 1 :(得分:0)
移动views'
中所有onCreateView(...)
的所有初始化,并在类级别声明这些视图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;
}
}
答案 2 :(得分: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);
}