我正在尝试为 Android 应用程序开发一个聊天室。
在第一个屏幕上,list view
显示 a。单击 中的一个项目时listview
,将启动一个活动,其中我显示了两个称为Chat
和的按钮Draw
。当我单击聊天时,我将启动另一个名为 asDisplayMessageActiviy
的活动,其中有一个区域 forEditText
和一个名为 as 的按钮Send
。
但是,当我单击 时Chat
,我收到以下错误 ::
Unfortunately My Fist App has stopped working
。
我正在使用以下代码行:
SingleListActivity.java
:
//chatMessage is the xfunction to be run on clicking on chat button
public void chatMessage(View view)
{
Intent intent_chat = new Intent(this, DisplayMessageActivity.class);
//EditText editText = (EditText) findViewById(R.id.edit_message);
//String message = editText.getText().toString();
//intent_chat.putExtra(EXTRA_MESSAGE, message);
startActivity(intent_chat);
}
DisplayMessageActivity.java
:
public class DisplayMessageActivity extends Activity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
public void displayChat(View view)
{
onResume();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
TextView text = (TextView) findViewById(R.id.chatWindow);
EditText editBox = (EditText)findViewById(R.id.chatBox);
String str = text.getText().toString();
text.setText(str +"\n"+editBox.getText().toString());
editBox.setText("");
editBox.setHint("Type in here");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
我在 LogCat 中收到以下错误:
01-30 10:37:40.055: E/AndroidRuntime(724): FATAL EXCEPTION: main
01-30 10:37:40.055: E/AndroidRuntime(724): java.lang.IllegalStateException: Could not execute method of the activity
01-30 10:37:40.055: E/AndroidRuntime(724): at android.view.View$1.onClick(View.java:3597)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.view.View.performClick(View.java:4202)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.view.View$PerformClick.run(View.java:17340)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.os.Handler.handleCallback(Handler.java:725)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.os.Handler.dispatchMessage(Handler.java:92)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.os.Looper.loop(Looper.java:137)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-30 10:37:40.055: E/AndroidRuntime(724): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 10:37:40.055: E/AndroidRuntime(724): at java.lang.reflect.Method.invoke(Method.java:511)
01-30 10:37:40.055: E/AndroidRuntime(724): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-30 10:37:40.055: E/AndroidRuntime(724): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-30 10:37:40.055: E/AndroidRuntime(724): at dalvik.system.NativeStart.main(Native Method)
01-30 10:37:40.055: E/AndroidRuntime(724): Caused by: java.lang.reflect.InvocationTargetException
01-30 10:37:40.055: E/AndroidRuntime(724): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 10:37:40.055: E/AndroidRuntime(724): at java.lang.reflect.Method.invoke(Method.java:511)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.view.View$1.onClick(View.java:3592)
01-30 10:37:40.055: E/AndroidRuntime(724): ... 11 more
01-30 10:37:40.055: E/AndroidRuntime(724): 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?
01-30 10:37:40.055: E/AndroidRuntime(724): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.app.Activity.startActivityForResult(Activity.java:3370)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.app.Activity.startActivityForResult(Activity.java:3331)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.app.Activity.startActivity(Activity.java:3566)
01-30 10:37:40.055: E/AndroidRuntime(724): at android.app.Activity.startActivity(Activity.java:3534)
01-30 10:37:40.055: E/AndroidRuntime(724): at com.example.myfirstapp.SingleListActivity.chatMessage(SingleListActivity.java:42)
01-30 10:37:40.055: E/AndroidRuntime(724): ... 14 more
AndroidManifet.xml
::
<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="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SingleListItem"
android:label="@string/title_activity_single_list_item" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SingleListActivity"
android:label="@string/title_activity_single_list" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.appfirst.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
有人可以帮我解决错误吗?谢谢和问候。