所以我一直在让它运行时遇到问题。这是一个直接从 android 基础培训中提取的教程。我收到以下错误消息:
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。
这是我的两个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 已停止”。有什么建议吗?