当我运行它并尝试使用 onclick 命令导航到不同的类时,我的应用程序崩溃了,这只是一个简单的意图,所以我对出了什么问题一无所知。
这是我的主要代码
package com.example.converter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@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 the user clicks the Temperature button */
public void send(View view) {
Intent intent = new Intent(this, Temp.class);
startActivity(intent);
}
}
这是尝试去的地方
package com.example.converter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class Temp extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_temp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_temp, menu);
return true;
}
}
显现
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.converter"
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=".Temp"
android:label="@string/title_activity_temp" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
和日志猫
> 12-05 10:14:11.544: E/AndroidRuntime(15769): Caused by: java.lang.NoSuchMethodException: Temperature
12-05 10:14:11.544: E/AndroidRuntime(15769): at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
12-05 10:14:11.544: E/AndroidRuntime(15769): at java.lang.Class.getMethod(Class.java:962)
12-05 10:14:11.544: E/AndroidRuntime(15769): at android.view.View$1.onClick(View.java:2145)
12-05 10:14:11.544: E/AndroidRuntime(15769): ... 11 more
12-05 10:21:56.234: D/AndroidRuntime(15860): Shutting down VM
12-05 10:21:56.234: W/dalvikvm(15860): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-05 10:21:56.234: E/AndroidRuntime(15860): FATAL EXCEPTION: main
12-05 10:21:56.234: E/AndroidRuntime(15860): java.lang.IllegalStateException: Could not find a method Temperature(View) in the activity class com.example.converter.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
12-05 10:21:56.234: E/AndroidRuntime(15860): at android.view.View$1.onClick(View.java:2152)
12-05 10:21:56.234: E/AndroidRuntime(15860): at android.view.View.performClick(View.java:2506)
12-05 10:21:56.234: E/AndroidRuntime(15860): at android.view.View$PerformClick.run(View.java:9112)
12-05 10:21:56.234: E/AndroidRuntime(15860): at android.os.Handler.handleCallback(Handler.java:587)
12-05 10:21:56.234: E/AndroidRuntime(15860): at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 10:21:56.234: E/AndroidRuntime(15860): at android.os.Looper.loop(Looper.java:130)
12-05 10:21:56.234: E/AndroidRuntime(15860): at android.app.ActivityThread.main(ActivityThread.java:3835)
12-05 10:21:56.234: E/AndroidRuntime(15860): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 10:21:56.234: E/AndroidRuntime(15860): at java.lang.reflect.Method.invoke(Method.java:507)
12-05 10:21:56.234: E/AndroidRuntime(15860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
12-05 10:21:56.234: E/AndroidRuntime(15860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
12-05 10:21:56.234: E/AndroidRuntime(15860): at dalvik.system.NativeStart.main(Native Method)
12-05 10:21:56.234: E/AndroidRuntime(15860): Caused by: java.lang.NoSuchMethodException: Temperature
12-05 10:21:56.234: E/AndroidRuntime(15860): at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
12-05 10:21:56.234: E/AndroidRuntime(15860): at java.lang.Class.getMethod(Class.java:962)
12-05 10:21:56.234: E/AndroidRuntime(15860): at android.view.View$1.onClick(View.java:2145)
12-05 10:21:56.234: E/AndroidRuntime(15860): ... 11 more