Luke Villanueva
Luke Villanueva

Reputation: 2070

Displaying Imageview using Java Code

I need to display images from res/drawable folder and put it in a imageview. I've done some research and implemented them. It only displays blank screen but no image.

My xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/mainlayout"
    />
</LinearLayout>

Java code:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {   
        String value = extras.getString("FaType");

        if(value.equals("AnimalBite"))
        {
            LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainlayout);

            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.medrem);
            LayoutParams imageViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            imageView.setLayoutParams(imageViewLayoutParams);

            mainLayout.addView(imageView);

        }
}

CATLOG

08-21 20:54:35.698: E/Trace(684): error opening trace file: No such file or directory (2)
08-21 20:54:36.618: D/dalvikvm(684): GC_FOR_ALLOC freed 227K, 4% free 8142K/8455K, paused 123ms, total 142ms
08-21 20:54:36.948: D/dalvikvm(684): GC_CONCURRENT freed 196K, 4% free 8383K/8711K, paused 35ms+25ms, total 138ms
08-21 20:54:37.978: I/Choreographer(684): Skipped 199 frames!  The application may be doing too much work on its main thread.
08-21 20:54:38.158: D/gralloc_goldfish(684): Emulator without GPU emulation detected.
08-21 20:54:39.318: I/Choreographer(684): Skipped 64 frames!  The application may be doing too much work on its main thread.
08-21 20:54:47.318: I/Choreographer(684): Skipped 77 frames!  The application may be doing too much work on its main thread.
08-21 20:54:48.138: I/Choreographer(684): Skipped 45 frames!  The application may be doing too much work on its main thread.
08-21 20:54:49.798: D/AndroidRuntime(684): Shutting down VM
08-21 20:54:49.798: W/dalvikvm(684): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-21 20:54:49.848: E/AndroidRuntime(684): FATAL EXCEPTION: main
08-21 20:54:49.848: E/AndroidRuntime(684): java.lang.RuntimeException: Unable to start activity ComponentInfo{dr.droid/dr.droid.ImageViewer}: java.lang.NullPointerException
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.os.Looper.loop(Looper.java:137)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-21 20:54:49.848: E/AndroidRuntime(684):  at java.lang.reflect.Method.invokeNative(Native Method)
08-21 20:54:49.848: E/AndroidRuntime(684):  at java.lang.reflect.Method.invoke(Method.java:511)
08-21 20:54:49.848: E/AndroidRuntime(684):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-21 20:54:49.848: E/AndroidRuntime(684):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 20:54:49.848: E/AndroidRuntime(684):  at dalvik.system.NativeStart.main(Native Method)
08-21 20:54:49.848: E/AndroidRuntime(684): Caused by: java.lang.NullPointerException
08-21 20:54:49.848: E/AndroidRuntime(684):  at dr.droid.ImageViewer.onCreate(ImageViewer.java:29)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.Activity.performCreate(Activity.java:5008)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-21 20:54:49.848: E/AndroidRuntime(684):  ... 11 more
08-21 20:54:52.268: I/Process(684): Sending signal. PID: 684 SIG: 9

Any ideas? I also need to know the problem of this code. Because whenever I try to addimages on my drawable folder and use it at this

imageView.setImageResource(R.drawable.medrem)

It doesn't seem to recognize the image. Help please?

Upvotes: 0

Views: 902

Answers (3)

user1616356
user1616356

Reputation:

add this line in your code

setContentView(R.layout.your layout name);

and it's good to go.

Upvotes: 0

Anders Metnik
Anders Metnik

Reputation: 6247

You have no setcontentview ?

That means that your MainLayout havn't been inflated, which results in the nullpointer exception thrown in your logfile.

Either inflate it manually or setContentView(R.layout.xxxx);

Upvotes: 0

user370305
user370305

Reputation: 109257

Change your if condition from if(value == "AnimalBite")

to

if(value.equals("AnimalBite"))

to compare two strings always use .equals() of String class..

Upvotes: 3

Related Questions