简体   繁体   English

Android-启动新活动时出现空指针异常错误

[英]Android - Null Pointer Exception error when launching new activity

The following code accepts an intent from a listview that contains a listview. 以下代码接受包含列表视图的列表视图的意图。 This activity captures the title of the listview selected, and then launches this activity, passing a string intent of the title. 此活动捕获所选列表视图的标题,然后启动此活动,并传递标题的字符串意图。 Before this activity loads, it crashes and logcat reports a 'Null Pointer Exception'. 在加载此活动之前,它会崩溃并且logcat报告“空指针异常”。 The items in the string array are as follows: 字符串数组中的项目如下:

[0][.] = Id, starting at 0
[1][.] = name, the name as shown on the listview
[2][.] = description

ect

This is the view activity 这是视图活动

 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);

I'm 99% sure the sending side is correct, so I have not posted it. 我99%确信发送方是正确的,所以我还没有发布。 I am happy to post it if you guys think theres something wrong with it. 如果你们认为它有问题,我很乐意将其发布。 Thank you HEAPS! 谢谢大家!

EDIT: Logcat: 编辑: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

EDIT AGAIN: (For Babibu) 再次编辑:(针对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

您忘记初始化,请检查您的代码

Unfortanatly the log cat is not helping us. 不幸的是,日志猫并没有帮助我们。

Try running this code: 尝试运行以下代码:

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();

        }

    }
}

I added a testNull method here to help us find the problem. 我在这里添加了一个testNull方法来帮助我们发现问题。 How ever a much better solution would be if you could just debug it. 如果您可以对其进行调试,那么更好的解决方案将是。

Your Challenges.chalarray is not initialised, so the first time you're trying to access it you get null. 您的Challenges.chalarray尚未初始化,因此,第一次尝试访问它时,您将获得null。 Change the initialisation to: 将初始化更改为:

public class Challenges {
    public static String[][] chalarray = {
        { "0", "Challenge1", "Sample Description", "5", "base" },
        { "1", "Challenge2?", "Sample Description", "4", "base"},
        //etc
    };
}

Also, I'd change the while loop to run through all the elements in chalarray and to break from the loop when correct id is found: 另外,我将更改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;
}

Just keep in mind that if the id is not found, intvalue will be 0 from initialization, so in the following lines make sure you handle that, in other words, do you want to display the details of the element 0: 请记住,如果未找到id,则初始化时intvalue将为0,因此在以下几行中请确保您已进行处理,换句话说,是否要显示元素0的详细信息:

title.setText(Challenges.chalarray[intvalue][1]);
description.setText(Challenges.chalarray[intvalue][2]);
String temp = Challenges.chalarray[intvalue][4];
String s1 = Challenges.chalarray[intvalue][3];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM