活动之间的沟通

时间:2013-03-06 10:00:06

标签: android imageview android-activity

我有一个活动让我们说活动A.

public class ActivityA extends Activity{
static int id_original;
.
.
.
id_original = R.drawable.afra_o;
}

afra_o是另一个活动布局的ImageView。让我们说活动B.在活动A中,我想将id_original设置为某个drawable,然后我想通过引用活动B中的id_original来更改B的ImageView。

public class ActivityB extends Activity {
ImageView view_correct = (ImageView)findViewById(R.id.image_original);
view_correct.setImageResource(ActivityA.id_original);
setContentView(R.layout.activity_B);

喜欢这个。但是我得到了像这样的错误

03-06 11:57:55.845: E/AndroidRuntime(10791): FATAL EXCEPTION: main
03-06 11:57:55.845: E/AndroidRuntime(10791): java.lang.RuntimeException: Unable to start    activity      ComponentInfo{com.example.turkishlogoquiz/com.example.turkishlogoquiz.CorrectActivity}:     java.lang.NullPointerException
03-06 11:57:55.845: E/AndroidRuntime(10791):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at android.os.Looper.loop(Looper.java:137)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at java.lang.reflect.Method.invokeNative(Native Method)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at java.lang.reflect.Method.invoke(Method.java:511)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at dalvik.system.NativeStart.main(Native Method)
03-06 11:57:55.845: E/AndroidRuntime(10791): Caused by: java.lang.NullPointerException
03-06 11:57:55.845: E/AndroidRuntime(10791):    at com.example.turkishlogoquiz.CorrectActivity.onCreate(CorrectActivity.java:20)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at android.app.Activity.performCreate(Activity.java:5206)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-06 11:57:55.845: E/AndroidRuntime(10791):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)

请帮助。

2 个答案:

答案 0 :(得分:1)

似乎ImageView view_correct = (ImageView)findViewById(R.id.image_original);返回空指针。在通过调用findViewById()设置内容视图之前,您无法呼叫setContentView(R.layout.activity_B);

检查出来:http://developer.android.com/reference/android/app/Activity.html

  • onCreate(Bundle)是您初始化活动的地方。最重要的是,在这里,您通常会使用定义UI的布局资源调用setContentView(int),并使用findViewById(int)检索该UI中需要以编程方式进行交互的窗口小部件。

答案 1 :(得分:1)

我处理了这个问题。我使用intent.putExtra将数据从一个活动传递到另一个活动。通过使用它,我设法动态地根据活动A设置活动B的图像。谢谢你的答案。