我正在从官方android网站上学习android。在我习惯于在文本框中输入数据并点击按钮a以及用于在不同屏幕上打印的数据之前,它确实很好。
但在我尝试自定义代码后,添加菜单选项。功能不再有效。当我点击按钮时,应用程序现在崩溃了。谁能告诉我哪里出错?
我的MainActivity.java文件如下。
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
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.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSettings() {
// Later
}
private void openSearch() {
// Later
}
}
当我尝试在选项卡上运行应用程序时,我收到以下错误
09-12 14:57:58.702: D/libEGL(5349): loaded /system/lib/egl/libGLES_android.so
09-12 14:57:58.712: D/libEGL(5349): loaded /system/lib/egl/libEGL_tegra.so
09-12 14:57:58.722: D/libEGL(5349): loaded /system/lib/egl/libGLESv1_CM_tegra.so
09-12 14:57:58.732: D/libEGL(5349): loaded /system/lib/egl/libGLESv2_tegra.so
09-12 14:57:58.782: D/OpenGLRenderer(5349): Enabling debug mode 0
09-12 14:58:07.662: D/AndroidRuntime(5349): Shutting down VM
09-12 14:58:07.662: W/dalvikvm(5349): threadid=1: thread exiting with uncaught exception (group=0x40a3e1f8)
09-12 14:58:07.672: E/AndroidRuntime(5349): FATAL EXCEPTION: main
09-12 14:58:07.672: E/AndroidRuntime(5349): java.lang.IllegalStateException: Could not execute method of the activity
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.view.View$1.onClick(View.java:3044)
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.view.View.performClick(View.java:3511)
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.view.View$PerformClick.run(View.java:14105)
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.os.Handler.handleCallback(Handler.java:605)
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.os.Looper.loop(Looper.java:137)
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.app.ActivityThread.main(ActivityThread.java:4446)
09-12 14:58:07.672: E/AndroidRuntime(5349): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 14:58:07.672: E/AndroidRuntime(5349): at java.lang.reflect.Method.invoke(Method.java:511)
09-12 14:58:07.672: E/AndroidRuntime(5349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-12 14:58:07.672: E/AndroidRuntime(5349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-12 14:58:07.672: E/AndroidRuntime(5349): at dalvik.system.NativeStart.main(Native Method)
09-12 14:58:07.672: E/AndroidRuntime(5349): Caused by: java.lang.reflect.InvocationTargetException
09-12 14:58:07.672: E/AndroidRuntime(5349): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 14:58:07.672: E/AndroidRuntime(5349): at java.lang.reflect.Method.invoke(Method.java:511)
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.view.View$1.onClick(View.java:3039)
09-12 14:58:07.672: E/AndroidRuntime(5349): ... 11 more
09-12 14:58:07.672: E/AndroidRuntime(5349): 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?
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.app.Activity.startActivityForResult(Activity.java:3190)
09-12 14:58:07.672: E/AndroidRuntime(5349): at android.app.Activity.startActivity(Activity.java:3297)
09-12 14:58:07.672: E/AndroidRuntime(5349): at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:38)
09-12 14:58:07.672: E/AndroidRuntime(5349): ... 14 more
09-12 15:03:07.712: I/Process(5349): Sending signal. PID: 5349 SIG: 9
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:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
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="17" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo" >
<activity
android:name="com.example.myfirstapp.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>
</application>
</manifest>
答案 0 :(得分:2)
将您的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="17" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo" >
<activity
android:name="com.example.myfirstapp.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=".DisplayMessageActivity"></activity>
</application>
答案 1 :(得分:1)
将此添加到您的清单
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity" />