我刚开始学习Android编程。 我创建了一个带有空白活动的新项目(API 19),并尝试添加如下ImageView: (在MainActivity的onCreate())
setContentView(R.layout.activity_main);
View view = (View) findViewById(R.layout.activity_main);
LinearLayout picLL = new LinearLayout(MainActivity.this);
picLL.layout(0, 0, 100, 100);
picLL.setLayoutParams(new LayoutParams(100, 100));
picLL.setOrientation(LinearLayout.HORIZONTAL);
((ViewGroup) view).addView(picLL); // <- This is line 36
ImageView myImage = new ImageView(this);
myImage.setImageResource(R.drawable.ic_launcher);
picLL.addView(myImage);
然而,应用程序在发布后立即崩溃。 我从Stackoverflow尝试了很多其他答案,但都没有奏效。似乎有一些我想念的东西......
这是LogCat输出:
04-17 10:02:35.611: E/AndroidRuntime(1402): FATAL EXCEPTION: main
04-17 10:02:35.611: E/AndroidRuntime(1402): Process: com.example.test, PID: 1402
04-17 10:02:35.611: E/AndroidRuntime(1402): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
04-17 10:02:35.611: E/AndroidRuntime(1402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-17 10:02:35.611: E/AndroidRuntime(1402): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-17 10:02:35.611: E/AndroidRuntime(1402): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-17 10:02:35.611: E/AndroidRuntime(1402): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-17 10:02:35.611: E/AndroidRuntime(1402): at android.os.Handler.dispatchMessage(Handler.java:102)
04-17 10:02:35.611: E/AndroidRuntime(1402): at android.os.Looper.loop(Looper.java:136)
04-17 10:02:35.611: E/AndroidRuntime(1402): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-17 10:02:35.611: E/AndroidRuntime(1402): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 10:02:35.611: E/AndroidRuntime(1402): at java.lang.reflect.Method.invoke(Method.java:515)
04-17 10:02:35.611: E/AndroidRuntime(1402): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-17 10:02:35.611: E/AndroidRuntime(1402): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-17 10:02:35.611: E/AndroidRuntime(1402): at dalvik.system.NativeStart.main(Native Method)
04-17 10:02:35.611: E/AndroidRuntime(1402): Caused by: java.lang.NullPointerException
04-17 10:02:35.611: E/AndroidRuntime(1402): at com.example.test.MainActivity.onCreate(MainActivity.java:36)
04-17 10:02:35.611: E/AndroidRuntime(1402): at android.app.Activity.performCreate(Activity.java:5231)
04-17 10:02:35.611: E/AndroidRuntime(1402): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-17 10:02:35.611: E/AndroidRuntime(1402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-17 10:02:35.611: E/AndroidRuntime(1402): ... 11 more
04-17 10:02:38.771: I/Process(1402): Sending signal. PID: 1402 SIG: 9
答案 0 :(得分:15)
之所以发生这种情况,是因为您试图通过布局ID来查找View
。
View view = (View) findViewById(R.layout.activity_main);
您有两种选择:
您可以在activity_main.xml
中为根元素添加任何ID。例如,它将是root_layout_id
。然后你可以找到它:
View view = (View) findViewById(R.id.root_layout_id);
第二个选项是以编程方式创建布局。像这样改变你的onCreate()方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout picLL = new LinearLayout(this);
picLL.layout(0, 0, 100, 100);
picLL.setLayoutParams(new LayoutParams(100, 100));
picLL.setOrientation(LinearLayout.HORIZONTAL);
ImageView myImage = new ImageView(this);
myImage.setImageResource(R.drawable.ic_launcher);
picLL.addView(myImage);
setContentView(picLL);
}