我正在尝试为Android应用程序开发聊天室。
在第一个屏幕上,显示list view
。点击listview
中的某个项目后,会启动一项活动,其中我显示两个名为Chat
和Draw
的按钮。当我点击聊天时,我正在启动另一个名为DisplayMessageActiviy
的活动,其中有一个EditText
区域和一个名为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>
有人可以帮我解决错误吗?谢谢和问候。
答案 0 :(得分:2)
您必须使用您的问题发布完整的堆栈跟踪(logcat错误)和完整代码
无论如何只是假设我认为问题在这里(假设 - 需要更多细节)
public void displayChat(View view)
{
onResume();
}
您永远不能在Android活动中明确调用onResume()
..
创建一个具有UI更新代码的函数,将其写入displayChat(View view)
像,
public void displayChat(View view)
{
updateUI();
}
<强>更新强>
However, when I click on Chat, I am getting the following error :: Unfortunately My Fist App has stopped working
您确定在activity tag
AndroidManifest.xml
文件中插入了 DisplayMessageActivity 。