我对Android的写作完全不熟悉,我设法遇到了一个无法解决的问题。我尝试在网上搜索,但我可以找到任何可以帮助我了解正在发生的事情......
该程序应该将一个字符串放入可编辑的文本字段,然后只需按一个按钮就可以显示它。按下按钮后程序崩溃了。这既可以在仿真器上也可以在实际设备上使用。
main.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
答案 0 :(得分: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
答案 1 :(得分:2)
而不是
<activity android:name="com.example.myapp.DisplayMessageActivity" />
使用
<activity android:name=".DisplayMessageActivity" />