以下代码接受包含listview的listview中的intent。此活动捕获所选列表视图的标题,然后启动此活动,传递标题的字符串意图。在此活动加载之前,它崩溃并且logcat报告“空指针异常”。字符串数组中的项目如下:
[0][.] = Id, starting at 0
[1][.] = name, the name as shown on the listview
[2][.] = description
等等
这是视图活动
public class view extends HomeActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Bundle b = getIntent().getExtras();
String value = b.getString("index");
TextView title = (TextView) findViewById(R.id.textView1);
TextView description = (TextView) findViewById(R.id.textView2);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
RatingBar rb = (RatingBar) findViewById(R.id.ratingBar1);
int tempPosition = 0;
int intvalue = 0;
//Find the id of the item[0], given the name[1]
while(value!=Challenges.chalarray[tempPosition][1]){
if (Challenges.chalarray[tempPosition][1].contains(value)){
String s = Challenges.chalarray[tempPosition][0];
intvalue = Integer.parseInt(s);
}
tempPosition = tempPosition + 1;
}
title.setText(Challenges.chalarray[intvalue][1]);
description.setText(Challenges.chalarray[intvalue][2]);
String temp = Challenges.chalarray[intvalue][4];
String s1 = Challenges.chalarray[intvalue][3];
float f1 = Float.parseFloat(s1);
int resourceId = getResources().getIdentifier (temp, "drawable", "com.tim.listviewapp");
iv.setImageResource(resourceId);
rb.setRating(f1);
我99%肯定发送方是正确的,所以我没有发布它。如果你们认为它有问题,我很乐意发布它。谢谢你好!
编辑: logcat的:
01-02 03:18:48.789: D/AndroidRuntime(1194): Shutting down VM
01-02 03:18:48.789: W/dalvikvm(1194): threadid=1: thread exiting with uncaught exception (group=0xb4b00b90)
01-02 03:18:48.849: E/AndroidRuntime(1194): FATAL EXCEPTION: main
01-02 03:18:48.849: E/AndroidRuntime(1194): Process: com.tim.listviewapp, PID: 1194
01-02 03:18:48.849: E/AndroidRuntime(1194): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tim.listviewapp/com.tim.listviewapp.View}: java.lang.NullPointerException
01-02 03:18:48.849: E/AndroidRuntime(1194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-02 03:18:48.849: E/AndroidRuntime(1194): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-02 03:18:48.849: E/AndroidRuntime(1194): at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-02 03:18:48.849: E/AndroidRuntime(1194): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-02 03:18:48.849: E/AndroidRuntime(1194): at android.os.Handler.dispatchMessage(Handler.java:102)
01-02 03:18:48.849: E/AndroidRuntime(1194): at android.os.Looper.loop(Looper.java:137)
01-02 03:18:48.849: E/AndroidRuntime(1194): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-02 03:18:48.849: E/AndroidRuntime(1194): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 03:18:48.849: E/AndroidRuntime(1194): at java.lang.reflect.Method.invoke(Method.java:515)
01-02 03:18:48.849: E/AndroidRuntime(1194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-02 03:18:48.849: E/AndroidRuntime(1194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-02 03:18:48.849: E/AndroidRuntime(1194): at dalvik.system.NativeStart.main(Native Method)
01-02 03:18:48.849: E/AndroidRuntime(1194): Caused by: java.lang.NullPointerException
01-02 03:18:48.849: E/AndroidRuntime(1194): at com.tim.listviewapp.View.onCreate(View.java:35)
01-02 03:18:48.849: E/AndroidRuntime(1194): at android.app.Activity.performCreate(Activity.java:5243)
01-02 03:18:48.849: E/AndroidRuntime(1194): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-02 03:18:48.849: E/AndroidRuntime(1194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-02 03:18:48.849: E/AndroidRuntime(1194): ... 11 more
再次编辑:(对于Babibu)
01-02 03:51:15.760: W/dalvikvm(883): threadid=1: thread exiting with uncaught exception (group=0xb4a1db90)
01-02 03:51:15.820: E/AndroidRuntime(883): FATAL EXCEPTION: main
01-02 03:51:15.820: E/AndroidRuntime(883): Process: com.tim.listviewapp, PID: 883
01-02 03:51:15.820: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tim.listviewapp/com.tim.listviewapp.View}: java.lang.NullPointerException
01-02 03:51:15.820: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-02 03:51:15.820: E/AndroidRuntime(883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-02 03:51:15.820: E/AndroidRuntime(883): at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-02 03:51:15.820: E/AndroidRuntime(883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-02 03:51:15.820: E/AndroidRuntime(883): at android.os.Handler.dispatchMessage(Handler.java:102)
01-02 03:51:15.820: E/AndroidRuntime(883): at android.os.Looper.loop(Looper.java:137)
01-02 03:51:15.820: E/AndroidRuntime(883): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-02 03:51:15.820: E/AndroidRuntime(883): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 03:51:15.820: E/AndroidRuntime(883): at java.lang.reflect.Method.invoke(Method.java:515)
01-02 03:51:15.820: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-02 03:51:15.820: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-02 03:51:15.820: E/AndroidRuntime(883): at dalvik.system.NativeStart.main(Native Method)
01-02 03:51:15.820: E/AndroidRuntime(883): Caused by: java.lang.NullPointerException
01-02 03:51:15.820: E/AndroidRuntime(883): at com.tim.listviewapp.View.onCreate(View.java:44)
01-02 03:51:15.820: E/AndroidRuntime(883): at android.app.Activity.performCreate(Activity.java:5243)
01-02 03:51:15.820: E/AndroidRuntime(883): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-02 03:51:15.820: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-02 03:51:15.820: E/AndroidRuntime(883): ... 11 more
01-02 03:51:19.270: I/Process(883): Sending signal. PID: 883 SIG: 9
答案 0 :(得分:0)
您忘了初始化,检查您的代码
答案 1 :(得分:0)
不幸的是,原木猫并没有帮助我们。
尝试运行此代码:
public class view extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Bundle b = getIntent().getExtras();
String value = b.getString("index");
TextView title = (TextView) findViewById(R.id.textView1);
TextView description = (TextView) findViewById(R.id.textView2);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
RatingBar rb = (RatingBar) findViewById(R.id.ratingBar1);
isNull(title);
isNull(description);
isNull(iv);
isNull(rb);
int tempPosition = 0;
int intvalue = 0;
isNull(Challenges.chalarray);
isNull(Challenges.chalarray[0]);
isNull(Challenges.chalarray[1]);
//Find the id of the item[0], given the name[1]
while(value!=Challenges.chalarray[tempPosition][1]){
if (Challenges.chalarray[tempPosition][1].contains(value)){
String s = Challenges.chalarray[tempPosition][0];
intvalue = Integer.parseInt(s);
}
tempPosition = tempPosition + 1;
}
isNull(Challenges.chalarray[0]);
isNull(Challenges.chalarray[1]);
isNull(Challenges.chalarray[2]);
isNull(Challenges.chalarray[3]);
isNull(Challenges.chalarray[4]);
title.setText(Challenges.chalarray[intvalue][1]);
description.setText(Challenges.chalarray[intvalue][2]);
String temp = Challenges.chalarray[intvalue][4];
String s1 = Challenges.chalarray[intvalue][3];
float f1 = Float.parseFloat(s1);
int resourceId = getResources().getIdentifier (temp, "drawable", "com.tim.listviewapp");
iv.setImageResource(resourceId);
rb.setRating(f1);
}
private void isNull(Object title) {
if(title == null){
Log.d("MyTag", "this is your error:");
new Throwable("Exception log").printStackTrace();
}
}
}
我在这里添加了testNull
方法来帮助我们找到问题。如果你可以调试它,那么更好的解决方案呢。
答案 2 :(得分:0)
您的Challenges.chalarray
未初始化,因此当您第一次尝试访问它时,您将获得null。将初始化更改为:
public class Challenges {
public static String[][] chalarray = {
{ "0", "Challenge1", "Sample Description", "5", "base" },
{ "1", "Challenge2?", "Sample Description", "4", "base"},
//etc
};
}
另外,我会更改while loop
以运行chalarray中的所有元素,并在找到正确的id时从循环中断:
while(tempPosition < Challenges.chalarray.length) {
if (Challenges.chalarray[tempPosition][1].contains(value)) {
String s = Challenges.chalarray[tempPosition][0];
intvalue = Integer.parseInt(s);
break;
}
tempPosition = tempPosition + 1;
}
请记住,如果找不到id,初始化时intvalue将为0,所以在以下几行中确保你处理它,换句话说,你想要显示元素0的细节: / p>
title.setText(Challenges.chalarray[intvalue][1]);
description.setText(Challenges.chalarray[intvalue][2]);
String temp = Challenges.chalarray[intvalue][4];
String s1 = Challenges.chalarray[intvalue][3];