2

我对 Android 写作完全陌生,我设法遇到了一个(对我来说)无法解决的问题。我尝试在网上搜索,但我可以找到任何可以帮助我了解正在发生的事情的东西......

该程序应该将一个字符串放入一个可编辑的文本字段中,然后在按下按钮时简单地显示它。程序在按下按钮时崩溃。这既发生在模拟器上,也发生在实际设备上。

主.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button android:id="@+id/button_send"
        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.sanuuu.tutorial1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Tutorial1Activity"
            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.myapp.DisplayMessageActivity" />
    </application>

</manifest>

Tutorial1Activity.java(主要的东西):

package com.sanuuu.tutorial1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class Tutorial1Activity extends Activity {
/** Called when the activity is first created. */


public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";

@Override    
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void sendMessage(View viewo) {
    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);
}
}

DisplayMessageActivity.java:

package com.sanuuu.tutorial1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(Tutorial1Activity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    setContentView(textView);
}
}

来自 LogCat 的日志:

06-25 17:42:43.535: I/dalvikvm(708): threadid=3: reacting to signal 3
06-25 17:42:43.684: I/dalvikvm(708): Wrote stack traces to '/data/anr/traces.txt'
06-25 17:42:43.804: D/gralloc_goldfish(708): Emulator without GPU emulation detected.
06-25 17:43:06.204: D/AndroidRuntime(708): Shutting down VM
06-25 17:43:06.214: W/dalvikvm(708): threadid=1: thread exiting with uncaught exception     (group=0x409c01f8)
06-25 17:43:06.274: E/AndroidRuntime(708): FATAL EXCEPTION: main
06-25 17:43:06.274: E/AndroidRuntime(708): java.lang.IllegalStateException: Could not   execute method of the activity
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.view.View$1.onClick(View.java:3044)
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.view.View.performClick(View.java:3511)
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.view.View$PerformClick.run(View.java:14105)
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.os.Handler.handleCallback(Handler.java:605)
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.os.Looper.loop(Looper.java:137)
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.app.ActivityThread.main(ActivityThread.java:4424)
06-25 17:43:06.274: E/AndroidRuntime(708):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 17:43:06.274: E/AndroidRuntime(708):  at java.lang.reflect.Method.invoke(Method.java:511)
06-25 17:43:06.274: E/AndroidRuntime(708):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 17:43:06.274: E/AndroidRuntime(708):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 17:43:06.274: E/AndroidRuntime(708):  at dalvik.system.NativeStart.main(Native Method)
06-25 17:43:06.274: E/AndroidRuntime(708): Caused by: java.lang.reflect.InvocationTargetException
06-25 17:43:06.274: E/AndroidRuntime(708):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 17:43:06.274: E/AndroidRuntime(708):  at java.lang.reflect.Method.invoke(Method.java:511)
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.view.View$1.onClick(View.java:3039)
06-25 17:43:06.274: E/AndroidRuntime(708):  ... 11 more
06-25 17:43:06.274: E/AndroidRuntime(708): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.sanuuu.tutorial1/com.sanuuu.tutorial1.DisplayMessageActivity}; have you declared this activity in your AndroidManifest.xml?
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.app.Activity.startActivityForResult(Activity.java:3190)
06-25 17:43:06.274: E/AndroidRuntime(708):  at android.app.Activity.startActivity(Activity.java:3297)
06-25 17:43:06.274: E/AndroidRuntime(708):  at com.sanuuu.tutorial1.Tutorial1Activity.sendMessage(Tutorial1Activity.java:26)
06-25 17:43:06.274: E/AndroidRuntime(708):  ... 14 more
4

2 回答 2

3

Manifest.xml中注册DisplayMessageActivity

<activity android:name=".DisplayMessageActivity" />

或者

<activity android:name="com.sanuuu.tutorial1.DisplayMessageActivity" />

代替

<activity android:name="com.example.myapp.DisplayMessageActivity" />

因为你的包是com.sanuuu.tutorial1而不是com.example.myapp

于 2012-06-25T18:16:05.437 回答
2

代替

<activity android:name="com.example.myapp.DisplayMessageActivity" />

利用

<activity android:name=".DisplayMessageActivity" />
于 2012-06-25T18:17:13.897 回答