I just started to learn Android programming. I created a new project (API 19) with empty activity and tried to add ImageView as follows: (in 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);
However, the application crashes immediately after launch. I tried many other answers from Stackoverflow, but no one worked. Something seems to be missing me ...
This is the output of 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
source share