NullPointerException - 无法弄清楚什么是null

时间:2013-07-20 17:46:27

标签: android nullpointerexception

我有以下代码动态创建一些视图:

        for (int i = 0; i < z; i++) {
            LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = vi.inflate(R.layout.tag_table_row, null); //this is the template for each view

            // set xyz dynamically
            TextView textView = (TextView) v.findViewById(R.id.xyz); //id within the layout 
            textView.setText(xyz[i]);
            //set score dynamically
            TextView textView2 = (TextView) v.findViewById(R.id.score);
            textView2.setText(scores[i]);
            //set bar graph dynamically
            TagBar tagBar = (TagBar) findViewById(R.id.tagbar);
            double tag1score = Double.parseDouble(scores[i]);
            Log.i("score", scores[i]);
            double factor = 250;
            double len = tag1score*factor;
            int length = (int) len;
            Log.i("length", Integer.toString(length));
            tagBar.setBarLength(length);                

            // insert into main view
            View insertPoint = findViewById(R.id.tagTable);
            ((ViewGroup) insertPoint).addView(v, i, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        }

TagBar是我创建的一个类,它基本上绘制一个水平条,其长度由“得分”决定。除了行tagBar.setBarLength(length);之外,所有这些代码都可以正常工作。当这一行运行时,我得到NullPointerException并且应用程序崩溃。

据我所知,length已定义且不为空,tagBar已定义且不为空,setBarLength()方法在TagBar内定义类。为什么我得到NullPointerException?

setBarLength()TagBar类中定义如下:

    public void setBarLength(int n) {
    barLeng = n;
    invalidate();
    requestLayout();
}

这是我的Logcat:

07-20 12:33:52.029: E/AndroidRuntime(8075): FATAL EXCEPTION: main
07-20 12:33:52.029: E/AndroidRuntime(8075): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tforan.blobtag4/com.tforan.blobtag4.PlaceActivity}: java.lang.NullPointerException
07-20 12:33:52.029: E/AndroidRuntime(8075):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1852)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1873)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at android.os.Looper.loop(Looper.java:150)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at android.app.ActivityThread.main(ActivityThread.java:4358)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at java.lang.reflect.Method.invokeNative(Native Method)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at java.lang.reflect.Method.invoke(Method.java:507)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at dalvik.system.NativeStart.main(Native Method)
07-20 12:33:52.029: E/AndroidRuntime(8075): Caused by: java.lang.NullPointerException
07-20 12:33:52.029: E/AndroidRuntime(8075):     at com.tforan.blobtag4.PlaceActivity.onCreate(PlaceActivity.java:169)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
07-20 12:33:52.029: E/AndroidRuntime(8075):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
07-20 12:33:52.029: E/AndroidRuntime(8075):     ... 11 more

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:4)

当您取消引用空指针时会生成空指针异常 - 当某个位于左侧时。一片空白。所以唯一可能导致它的是tagbar为null,假设你有正确的行。我确保您正在搜索的ID实际上在您的xml中。如果确实如此,那么我将在调试器下运行它。

编辑:你的意思是在活动的视图中搜索标签栏,而不是在循环中膨胀的视图中吗?这似乎可能是错误。所有其他发现都是v.findViewById,这个只是findViewById。

答案 1 :(得分:2)

检查此处 TagBar tagBar = (TagBar) findViewById(R.id.tagbar);

您将获得java.lang.NullPointerException

因为您的tagBar对象为空!