我正在关注Google网站上的开发指南:http://developer.android.com/training/basics/firstapp/starting-activity.html
但每当我按下模拟器中的按钮时,应用程序就会崩溃。 我在手机上试了一下,得到了同样的结果。这是我的代码 缩进在此处无法正常工作 主要活动:
package com.example.rishubs.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when Button is pressed */
public void sendMessage(View view) {
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);
}
}
我的XML:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rishubs.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
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/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
我的第二项活动
package com.example.rishubs.app;
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 Message from intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
更新了XML:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rishubs.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
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/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/app_name">
</activity>
<activity android:name="DisplayMessageActivity">
</activity>
</application>
</manifest>
logcat的:
07-15 18:48:26.605: E/AndroidRuntime(1454): FATAL EXCEPTION: main
07-15 18:48:26.605: E/AndroidRuntime(1454): java.lang.IllegalStateException: Could not find a method send message(View) in the activity class com.example.rishubs.app.MainActivity for onClick handler on view class android.widget.Button
07-15 18:48:26.605: E/AndroidRuntime(1454): at android.view.View$1.onClick(View.java:3031)
07-15 18:48:26.605: E/AndroidRuntime(1454): at android.view.View.performClick(View.java:3511)
07-15 18:48:26.605: E/AndroidRuntime(1454): at android.view.View$PerformClick.run(View.java:14105)
07-15 18:48:26.605: E/AndroidRuntime(1454): at android.os.Handler.handleCallback(Handler.java:605)
07-15 18:48:26.605: E/AndroidRuntime(1454): at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 18:48:26.605: E/AndroidRuntime(1454): at android.os.Looper.loop(Looper.java:137)
07-15 18:48:26.605: E/AndroidRuntime(1454): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-15 18:48:26.605: E/AndroidRuntime(1454): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 18:48:26.605: E/AndroidRuntime(1454): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 18:48:26.605: E/AndroidRuntime(1454): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-15 18:48:26.605: E/AndroidRuntime(1454): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-15 18:48:26.605: E/AndroidRuntime(1454): at dalvik.system.NativeStart.main(Native Method)
07-15 18:48:26.605: E/AndroidRuntime(1454): Caused by: java.lang.NoSuchMethodException: send message [class android.view.View]
07-15 18:48:26.605: E/AndroidRuntime(1454): at java.lang.Class.getConstructorOrMethod(Class.java:460)
07-15 18:48:26.605: E/AndroidRuntime(1454): at java.lang.Class.getMethod(Class.java:915)
07-15 18:48:26.605: E/AndroidRuntime(1454): at android.view.View$1.onClick(View.java:3024)
07-15 18:48:26.605: E/AndroidRuntime(1454): ... 11 more
答案 0 :(得分:2)
我看到你的错误。
java.lang.IllegalStateException: Could not find a method send message(View) in the activity class com.example.rishubs.app.MainActivity for onClick handler on view class android.widget.Button
转到layout / activity_main.xml文件,在Button View中确保
android:onClick="sendMessage"
在那里。根据LogCat你有android:onClick =“发送消息”而不是。所以有一个不需要的空间发送+消息,消息需要首字母大写。
基本上,您已将方法名称命名为sendMessage(View v),但已将其在XML文件中引用为“发送消息”。它们不一样:D
祝你好运答案 1 :(得分:0)
你已添加
<activity
android:name=".DisplayMessageActivity"
android:label="@string/app_name">
</activity>
<activity android:name="DisplayMessageActivity">
</activity>
替换为
<activity
android:name=".DisplayMessageActivity"
android:label="@string/app_name"> </activity>
你已经定义了Activity
两次......可能这是崩溃的问题。
编辑根据您的新LogCat
接触,例如,activity_main.xml
中的onclick方法名称,在edit_message
中,您有android:onClick="send message"
之类的属性将其替换为android:onClick="sendMessage"
答案 2 :(得分:-3)
而不是:
Intent intent = new Intent(this, DisplayMessageActivity.class);
试试,这是在你的MainActivity中:
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);