Guparasti
Guparasti

Reputation: 189

android app crashed onclick

my app crashes when i run it and try to navigate to a different class using onclick comand, its just a simple intent so im clueless as to whats wrong.

this is my main code

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

this is where in trying to go

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

 <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>

and the 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

Upvotes: 0

Views: 687

Answers (4)

CaptainStony
CaptainStony

Reputation: 51

This is the main code for you:

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

}

Upvotes: 0

Andrii Chernenko
Andrii Chernenko

Reputation: 10194

You have set android:onclick="Temperature" on the button you click in your layout xml, but the method name is "send". Change the method name to "send" in your layout xml:

android:onclick="send"

Upvotes: 1

Vinay
Vinay

Reputation: 2415

In your XML of UI if you are setting android:onclick="Temperature" then change it to android:onclick="send"

Upvotes: 1

Siddharth Lele
Siddharth Lele

Reputation: 27748

In this portion of your code,

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

You must have have the following attribute defined to your Button (I am assuming) in your activity_main.xml:

android:onClick="send"

Alternatively, you can assign the Button an id like this:

android:id="@+id/btnShow"

And in your 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);
    }
});

Upvotes: 0

Related Questions