我有以下代码可以动态创建一些视图:
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
任何帮助将不胜感激。谢谢!
最佳答案
当取消引用空指针时(在a的左侧),将生成一个空指针异常。一片空白。因此,唯一可能导致标签栏为null的情况是假设您的行正确。我将确保您要搜索的ID实际上在您的xml中。如果确实如此,那么我将在调试器下运行它。
编辑:您的意思是在活动视图中搜索标签栏,而不是在循环中膨胀的视图中搜索?这似乎可能是错误。其他所有查找内容均为v.findViewById,此仅为findViewById。