当用户点击graphic_a
时,我正尝试从我的简介屏幕导航到第二个屏幕。出于某种原因,当我点击graphic_a
时应用程序退出。
知道我做错了什么吗?
我在MainActivity.java
中有以下代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
// Find the View that shows the graphic_a
ImageView graphic_a = findViewById(R.id.graphic_a);
// Set a click listener on that View
graphic_a.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when graphic_a is clicked on.
@Override
public void onClick(View view) {
// Create a new intent to open the {@link SongAActivity}
Intent songAIntent = new Intent(MainActivity.this, SongAActivity.class);
// Start the new activity
startActivity(songAIntent);
}
});
}
}
因此用户点击了graphic_a,他们应该被发送到具有以下代码的SongAActivity.class:
public class SongAActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_layout);
ImageView imageView = findViewById(R.id.graphic_x);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.sgtpepper));
}
}
还有一个xml文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tan_background"
android:orientation="vertical"
tools:context="com.example.android.musicplayer.MainActivity">
<ImageView
android:id="@+id/graphic_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
我的logcat是:
04-18 23:01:36.568 27625-27625/? I/zygote64: Late-enabling -Xcheck:jni
04-18 23:01:36.641 27625-27638/? E/zygote64: Failed sending reply to debugger: Broken pipe
04-18 23:01:36.642 27625-27638/? I/zygote64: Debugger is no longer active
04-18 23:01:36.757 27625-27625/? I/InstantRun: starting instant run server: is main process
04-18 23:01:37.141 27625-27652/? D/OpenGLRenderer: HWUI GL Pipeline
04-18 23:01:48.710 27625-27652/com.example.android.musicplayer I/Adreno: QUALCOMM build : 2941438, I916dfac403
Build Date : 10/03/17
OpenGL ES Shader Compiler Version: EV031.21.02.00
Local Branch : O18A
Remote Branch :
Remote Branch :
Reconstruct Branch :
04-18 23:01:48.716 27625-27652/com.example.android.musicplayer I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
04-18 23:01:48.720 27625-27652/com.example.android.musicplayer I/zygote64: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
04-18 23:01:48.720 27625-27652/com.example.android.musicplayer I/OpenGLRenderer: Initialized EGL, version 1.4
04-18 23:01:48.721 27625-27652/com.example.android.musicplayer D/OpenGLRenderer: Swap behavior 2
04-18 23:01:53.009 27625-27625/com.example.android.musicplayer D/AndroidRuntime: Shutting down VM
04-18 23:01:53.016 27625-27625/com.example.android.musicplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.musicplayer, PID: 27625
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.android.musicplayer/com.example.android.musicplayer.SongAActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1933)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1616)
at android.app.Activity.startActivityForResult(Activity.java:4487)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
at android.app.Activity.startActivityForResult(Activity.java:4445)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
at android.app.Activity.startActivity(Activity.java:4806)
at android.app.Activity.startActivity(Activity.java:4774)
at com.example.android.musicplayer.MainActivity$1.onClick(MainActivity.java:31)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:1)
从您的代码中,在MainActivity.java文件中,您应该更改:
// Find the View that shows the graphic_a
ImageView graphic_a = findViewById(R.id.graphic_a);
要强>
// Find the View that shows the graphic_a
ImageView graphic_a = findViewById(R.id.graphic_x);
或者只是将xml文件中的图片ID从 graphic_x 更改为 graphic_a 。
更新2:从您的日志cat中,您尚未在清单文件中声明SongAActivity。要解决此问题,请在清单文件中添加以下代码。
<activity android:name="com.example.android.musicplayer.SongAActivity"/>
然后再试一次。