Question

Im trying to display an Image from another class to my current class. I've been reading alot on questions on here as to how to do this.

From the error log, I gather that the context Im trying to pass is not "fetched" by the loading class and causing a Null Pointer..I've tried a ton of different variations, and approaches, too many to post here..so the code posted below is the cleanest and hopefully the easiest way to explain my approach..thanks in advance

The image "holder" class MyImage.java

  public class MyImage {

ImageView image3, image1;
    View image2;
    int right = R.id.image_holder;
    int wrong = R.drawable.ic_wrong;
  int layoutholder = R.id.layout_holder;
    Bitmap a;


   public View getView(Context context) {

     image1.setBackgroundResource(wrong);

    return getView(context);
    }

The TestLoadingImage.java class where Im trying to display the View

public class TestLoadingImage extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void myImageHandler (View view){

    MyImage image = new MyImage();
    View gView1 = image.getView(this);
    LinearLayout layout = (LinearLayout) findViewById(R.id.layout_holder);
    layout.addView(gView1);

}

}

Here is my main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
        android:id="@+id/myimgbtn"
        android:layout_height="wrap_content"
        android:onClick="myImageHandler"
        android:layout_width="fill_parent"
        android:text="Image Handler Button"/>


<LinearLayout
    android:id="@+id/layout_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

 <ImageView android:id="@+id/image_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:src="@drawable/right"/>
   </LinearLayout>

  </LinearLayout>

This builds, but it crashes crashes when I press the button to display the view.

`03-14 07:19:58.770: E/AndroidRuntime(4299):    at android.graph.MyImage.getView(MyImage.java:26)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.graph.TestLoadingImage.myImageHandler(TestLoadingImage.java:32)`

Entire Error log :

03-14 07:19:58.770: E/AndroidRuntime(4299): FATAL EXCEPTION: main
03-14 07:19:58.770: E/AndroidRuntime(4299): java.lang.IllegalStateException: Could not execute method of the activity
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.view.View$1.onClick(View.java:3591)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.view.View.performClick(View.java:4084)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.view.View$PerformClick.run(View.java:16966)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.os.Handler.handleCallback(Handler.java:615)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.os.Looper.loop(Looper.java:137)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at java.lang.reflect.Method.invoke(Method.java:511)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at dalvik.system.NativeStart.main(Native Method)
03-14 07:19:58.770: E/AndroidRuntime(4299): Caused by: java.lang.reflect.InvocationTargetException
03-14 07:19:58.770: E/AndroidRuntime(4299):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at java.lang.reflect.Method.invoke(Method.java:511)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.view.View$1.onClick(View.java:3586)
03-14 07:19:58.770: E/AndroidRuntime(4299):     ... 11 more
03-14 07:19:58.770: E/AndroidRuntime(4299): Caused by: java.lang.NullPointerException
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.graph.MyImage.getView(MyImage.java:26)
03-14 07:19:58.770: E/AndroidRuntime(4299):     at android.graph.TestLoadingImage.myImageHandler(TestLoadingImage.java:32)
03-14 07:19:58.770: E/AndroidRuntime(4299):     ... 14 more
Was it helpful?

Solution

The log shows a NullPointerException on line 26 of MyImage.java. Though you didn't specify which line that is, the issue is almost certainly image1.setBackgroundResource(wrong);.

In your MyImage class, you never instantiate any of your Views. Thus image1, image2, and image3 are all null. When you try to set the background resource of image1, you thus generate a NullPointerException. I'm not sure where you expect/want these Views to come from, but it isn't happening.

Though it isn't a problem right now because you are crashing before getView() returns, you will also have a problem with infinite recursion when due to return getView(context);.

OTHER TIPS

Try passing the application context to MyImage:

MyImage image = new MyImage( getApplicationContext() );
Button bt1 =m_activity.findViewById(R.id.button1);

In the above example where view bt1 is from another class.you can access bt1 by using class instance and get the id.Now,do what ever you want do with bt1 instance.

Note: Make Sure bt1 is public in another class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top