Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android programmatically add ImageView

I just started learning Android programming. I created a new project (API 19) with a blank activity and tried to add an ImageView like this: (in the MainActivity's 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 app crashes immediately after launch. I've tried numerous other answers from Stackoverflow, but none worked. There seems to be something I am missing...

This is the LogCat output:

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
like image 317
Atomix Avatar asked Apr 17 '14 14:04

Atomix


1 Answers

It happens because you are trying to find View by layout's id.

View view = (View) findViewById(R.layout.activity_main); 

You have two options: You can add any id for root element in activity_main.xml. For example it will be root_layout_id. Then you can find it like:

View view = (View) findViewById(R.id.root_layout_id); 

The second option is to create layout programatically at all. Change your onCreate() method like this:

@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);
    }
like image 157
olegr Avatar answered Nov 11 '22 02:11

olegr