所以我一直有问题让这个运行。这是一个直接从android基础培训中获取的教程。我收到以下错误消息:
05-11 06:59:39.457:W / dalvikvm(623):threadid = 1:线程退出时未捕获异常(group = 0x409c01f8) 05-11 06:59:39.617:E / AndroidRuntime(623):致命异常:主要 05-11 06:59:39.617:E / AndroidRuntime(623):java.lang.IllegalStateException:无法执行活动的方法 05-11 06:59:39.617:E / AndroidRuntime(623):在android.view.View $ 1.onClick(View.java:3044) 05-11 06:59:39.617:E / AndroidRuntime(623):在android.view.View.performClick(View.java:3511) 05-11 06:59:39.617:E / AndroidRuntime(623):在android.view.View $ PerformClick.run(View.java:14105) 05-11 06:59:39.617:E / AndroidRuntime(623):在android.os.Handler.handleCallback(Handler.java:605) 05-11 06:59:39.617:E / AndroidRuntime(623):在android.os.Handler.dispatchMessage(Handler.java:92) 05-11 06:59:39.617:E / AndroidRuntime(623):在android.os.Looper.loop(Looper.java:137) 05-11 06:59:39.617:E / AndroidRuntime(623):在android.app.ActivityThread.main(ActivityThread.java:4424) 05-11 06:59:39.617:E / AndroidRuntime(623):at java.lang.reflect.Method.invokeNative(Native Method) 05-11 06:59:39.617:E / AndroidRuntime(623):at java.lang.reflect.Method.invoke(Method.java:511) 05-11 06:59:39.617:E / AndroidRuntime(623):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 05-11 06:59:39.617:E / AndroidRuntime(623):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-11 06:59:39.617:E / AndroidRuntime(623):at dalvik.system.NativeStart.main(Native Method) 05-11 06:59:39.617:E / AndroidRuntime(623):引起:java.lang.reflect.InvocationTargetException 05-11 06:59:39.617:E / AndroidRuntime(623):at java.lang.reflect.Method.invokeNative(Native Method) 05-11 06:59:39.617:E / AndroidRuntime(623):at java.lang.reflect.Method.invoke(Method.java:511) 05-11 06:59:39.617:E / AndroidRuntime(623):在android.view.View $ 1.onClick(View.java:3039) 05-11 06:59:39.617:E / AndroidRuntime(623):......还有11个 05-11 06:59:39.617:E / AndroidRuntime(623):引起:android.content.ActivityNotFoundException:无法找到显式活动类{com.example.namespace / com.example.namespace.DisplayMessageActivity};你有没有在AndroidManifest.xml中声明这个活动? 05-11 06:59:39.617:E / AndroidRuntime(623):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508) 05-11 06:59:39.617:E / AndroidRuntime(623):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1384) 05-11 06:59:39.617:E / AndroidRuntime(623):在android.app.Activity.startActivityForResult(Activity.java:3190) 05-11 06:59:39.617:E / AndroidRuntime(623):在android.app.Activity.startActivity(Activity.java:3297) 05-11 06:59:39.617:E / AndroidRuntime(623):at com.example.namespace.MyFirstAppActivity.sendMessage(MyFirstAppActivity.java:25) 05-11 06:59:39.617:E / AndroidRuntime(623):... 14更多 05-11 06:59:40.477:I / dalvikvm(623):threadid = 3:对信号3作出反应
这是我的两个java文件,
package com.example.namespace;
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(MyFirstAppActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
和
package com.example.namespace;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MyFirstAppActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
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);
}
}
这是我的androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.namespace"
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="com.example.myapp.DisplayMessageActivity" />
<activity
android:name=".MyFirstAppActivity"
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>
和我的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>
每次我运行这个时,都会调用“sendMessage”,我得到“不幸的是,myFirstApp已经停止了”。有什么建议吗?
答案 0 :(得分:1)
我认为这应该是
android:name="com.example.myapp.DisplayMessageActivity"
更改为
android:name="com.example.namespace.DisplayMessageActivity"
因为没有包 com.example.myapp
答案 1 :(得分:1)
日志中的答案。 “你有没有在AndroidManifest.xml中声明这个活动?”
将活动DisplayMessageActivity添加到AndroidManifest.xml
<activity android:name=".DisplayMessageActivity" />