1

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

        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是我创建的一个类,它基本上绘制一个长度由“分数”确定的水平条。除了 line 之外,所有这些代码都可以正常工作tagBar.setBarLength(length);。当这条线运行时,我得到 aNullPointerException并且应用程序崩溃。

据我所知,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

任何帮助将不胜感激。谢谢!

4

2 回答 2

4

当您取消引用空指针时会生成空指针异常 - 当 . 一片空白。所以唯一可能导致它是标签栏为空,假设你有正确的行。我会确保您要搜索的 id 实际上在您的 xml 中。如果确实如此,那么我会在调试器下运行它。

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

于 2013-07-20T17:49:49.687 回答
2

在这里检查 TagBar tagBar = (TagBar) findViewById(R.id.tagbar);

你得到java.lang.NullPointerException

因为你的tagBar对象是 null !

于 2013-07-20T17:54:02.650 回答