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