我正在关注Android Developer网站上的Starting Another Activity教程,但每次尝试运行我的应用程序时都会崩溃。我一直无法找到我的错误,但在点击提交按钮后它总是崩溃。我对此非常陌生,不知道出了什么问题。
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myFirstApp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myFirstApp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myFirstApp.MainActivity" />
</activity>
</application>
</manifest>
MainActivity.java
package com.example.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
//import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//responds to user clicking button
public void sendMessage(View view){
Intent intent= new Intent();
intent.setClassName("com.example.myfirstapp", "com.example.myfirstapp.DisplayMessageActivity");
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
activity_main.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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context="com.example.myfirstapp.MainActivity" >
<EditText android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
DisplayMessageActivity.java
package com.example.myfirstapp;
import android.support.v7.app.ActionBarActivity;
//import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
//import android.view.LayoutInflater;
//import android.view.Menu;
import android.view.MenuItem;
//import android.view.View;
//import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView= new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和activity_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp.DisplayMessageActivity" >
<TextView android="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
logcat的
08-26 17:53:31.124: D/gralloc_goldfish(1278): Emulator without GPU emulation detected.
08-26 17:53:49.884: I/Choreographer(1278): Skipped 56 frames! The application may be doing too much work on its main thread.
08-26 17:53:53.164: D/AndroidRuntime(1278): Shutting down VM
08-26 17:53:53.164: W/dalvikvm(1278): threadid=1: thread exiting with uncaught exception (group=0xb2a36ba8)
08-26 17:53:53.194: E/AndroidRuntime(1278): FATAL EXCEPTION: main
08-26 17:53:53.194: E/AndroidRuntime(1278): Process: com.example.myfirstapp, PID: 1278
08-26 17:53:53.194: E/AndroidRuntime(1278): java.lang.IllegalStateException: Could not execute method of the activity
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.view.View$1.onClick(View.java:3823)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.view.View.performClick(View.java:4438)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.view.View$PerformClick.run(View.java:18422)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.os.Handler.handleCallback(Handler.java:733)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.os.Handler.dispatchMessage(Handler.java:95)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.os.Looper.loop(Looper.java:136)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-26 17:53:53.194: E/AndroidRuntime(1278): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 17:53:53.194: E/AndroidRuntime(1278): at java.lang.reflect.Method.invoke(Method.java:515)
08-26 17:53:53.194: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-26 17:53:53.194: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-26 17:53:53.194: E/AndroidRuntime(1278): at dalvik.system.NativeStart.main(Native Method)
08-26 17:53:53.194: E/AndroidRuntime(1278): Caused by: java.lang.reflect.InvocationTargetException
08-26 17:53:53.194: E/AndroidRuntime(1278): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 17:53:53.194: E/AndroidRuntime(1278): at java.lang.reflect.Method.invoke(Method.java:515)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.view.View$1.onClick(View.java:3818)
08-26 17:53:53.194: E/AndroidRuntime(1278): ... 11 more
08-26 17:53:53.194: E/AndroidRuntime(1278): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}; have you declared this activity in your AndroidManifest.xml?
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.app.Activity.startActivityForResult(Activity.java:3424)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.app.Activity.startActivityForResult(Activity.java:3385)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.app.Activity.startActivity(Activity.java:3627)
08-26 17:53:53.194: E/AndroidRuntime(1278): at android.app.Activity.startActivity(Activity.java:3595)
08-26 17:53:53.194: E/AndroidRuntime(1278): at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:46)
08-26 17:53:53.194: E/AndroidRuntime(1278): ... 14 more
答案 0 :(得分:3)
您可能需要将第二个活动添加到清单中。最常见的错误;)
是的,有证据:08-26 17:53:53.194: E/AndroidRuntime(1278): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}; have you declared this activity in your AndroidManifest.xml?
取消完全限定名称,改为使用.DisplayMessageActivity。