我试图将另一个班级的图像显示到我当前的班级。我一直在这里阅读很多关于如何做到这一点的问题。
从错误日志中,我收集到我试图传递的上下文不是由加载类“获取”并导致空指针..我尝试了很多不同的变体和方法,这里发布的太多了..所以下面发布的代码是解释我的方法的最干净,希望是最简单的方法..提前谢谢
图像“持有人”类 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);
}
我试图显示视图的 TestLoadingImage.java 类
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);
}
}
这是我的 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>
这会构建,但是当我按下按钮显示视图时它会崩溃。
`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): 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