Android应用程序崩溃onclick

时间:2012-12-05 16:29:24

标签: android eclipse

当我运行它时,我的应用程序崩溃并尝试使用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>

和logcat

> 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

4 个答案:

答案 0 :(得分:1)

您已在布局xml中单击的按钮上设置了android:onclick="Temperature",但方法名称为“send”。在布局xml中将方法名称更改为“send”:

android:onclick="send"

答案 1 :(得分:1)

如果您要设置android:onclick="Temperature",请在您的用户界面XML中将其更改为android:onclick="send"

答案 2 :(得分:0)

在代码的这一部分中,

//called when the user clicks the Temperature button */
public void send(View view) {
    Intent intent = new Intent(this, Temp.class);
    startActivity(intent);
}

您必须在Button中为activity_main.xml(我假设)定义以下属性:

android:onClick="send"

或者,您可以为Button分配如下ID:

android:id="@+id/btnShow"

在您的MainActivity

Button btnShow = (Button) findViewById(R.id.btnShow)
btnShow .setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
            Intent intent = new Intent(this, Temp.class);
            startActivity(intent);
    }
});

答案 3 :(得分:0)

这是您的主要代码:

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);
}

//called when the user clicks the Temperature button */
public void send(View view) {
    Intent intent = new Intent(this, Temp.class);
    startActivity(intent);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}