0

当我运行它并尝试使用 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
4

4 回答 4

1

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

android:onclick="send"
于 2012-12-05T16:33:48.320 回答
1

如果您正在设置,android:onclick="Temperature"则在 UI 的 XML 中将其更改为android:onclick="send"

于 2012-12-05T16:35:23.460 回答
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;
}

}
于 2014-01-03T09:58:32.670 回答
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);
    }
});
于 2012-12-05T16:34:25.733 回答