我正在关注 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>
日志猫:
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