我有以下代码可以动态创建一些视图:
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
任何帮助将不胜感激。谢谢!