2

Androidの公式WebサイトからAndroidを学んでいます。テキストボックスにデータを入力してボタンをクリックし、データを別の画面に印刷するのに使用していたときは、以前は問題なく動作していました。

しかし、コードをカスタマイズしようとした後、メニューオプションを追加しました。機能が動作しなくなりました。ボタンをクリックすると、アプリがクラッシュします。誰が私がどこで間違っているのかアドバイスできますか?

私の 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>
4

2 に答える 2