我在xml中添加了一个图像和一个按钮,如下所示
<Button
android:id="@+id/btnChangeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/buttonplayicon" />
点击按钮我要更改图像。下面是按钮
的main_activity onclick功能public void addListenerOnButton() {
image = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.btnChangeImage);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
image.setImageResource(R.drawable.player_pause);
}
});
}
如果我添加
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
image.setImageResource(R.drawable.player_pause);
}
});
在函数中,我收到消息“遗憾的应用程序停止了”
对此有任何想法吗?
编辑1
要查看logcat,如果我关注(在eclipse:Window&gt; show view&gt; other&gt; android&gt; Logcat)步骤我得到Logcat(已弃用)
编辑2 logcat:
06-12 13:37:43.006: E/AndroidRuntime(3159): Caused by: java.lang.NullPointerException
06-12 13:37:43.006: E/AndroidRuntime(3159): at com.example.play_audio_app.MainActivity.addListenerOnButton(MainActivity.java:38)
06-12 13:37:43.006: E/AndroidRuntime(3159): at com.example.play_audio_app.MainActivity.onCreate(MainActivity.java:30)
06-12 13:37:43.006: E/AndroidRuntime(3159): at android.app.Activity.performCreate(Activity.java:5231)
06-12 13:37:43.006: E/AndroidRuntime(3159): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-12 13:37:43.006: E/AndroidRuntime(3159): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-12 13:37:43.006: E/AndroidRuntime(3159): ... 11 more
06-12 13:38:41.816: D/AndroidRuntime(3304): Shutting down VM
06-12 13:38:41.816: W/dalvikvm(3304): threadid=1: thread exiting with uncaught exception (group=0x4164eba8)
06-12 13:38:41.816: E/AndroidRuntime(3304): FATAL EXCEPTION: main
06-12 13:38:41.816: E/AndroidRuntime(3304): Process: com.example.play_audio_app, PID: 3304
06-12 13:38:41.816: E/AndroidRuntime(3304): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.play_audio_app/com.example.play_audio_app.MainActivity}: java.lang.NullPointerException
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.os.Handler.dispatchMessage(Handler.java:102)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.os.Looper.loop(Looper.java:136)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-12 13:38:41.816: E/AndroidRuntime(3304): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 13:38:41.816: E/AndroidRuntime(3304): at java.lang.reflect.Method.invoke(Method.java:515)
06-12 13:38:41.816: E/AndroidRuntime(3304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-12 13:38:41.816: E/AndroidRuntime(3304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-12 13:38:41.816: E/AndroidRuntime(3304): at dalvik.system.NativeStart.main(Native Method)
06-12 13:38:41.816: E/AndroidRuntime(3304): Caused by: java.lang.NullPointerException
06-12 13:38:41.816: E/AndroidRuntime(3304): at com.example.play_audio_app.MainActivity.addListenerOnButton(MainActivity.java:38)
06-12 13:38:41.816: E/AndroidRuntime(3304): at com.example.play_audio_app.MainActivity.onCreate(MainActivity.java:30)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.Activity.performCreate(Activity.java:5231)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-12 13:38:41.816: E/AndroidRuntime(3304): ... 11 more
编辑3
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
addListenerOnButton();
}
答案 0 :(得分:3)
只需在XML中设置onClick
,就容易多了。
android:onClick="whatever"
然后在你的班上,
public void whatever(View v) {
// Do your stuff
}
你不需要这一切:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
image.setImageResource(R.drawable.player_pause);
}
});