简体   繁体   中英

Android app crashes after adding second button

For some reason I cannot work out, when I add the second button and onClick event "pi", my application will not run. I might just be having a dull moment, and should probably create an 'onClick' method with the two inside. Any help much appreciated.

package app.example.task01;

import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MActivity extends Activity {

    //Variable Instantiation 
    EditText name;
    Button push;
    Button pi;
    TextView display;
    Ringtone rTone;
    Uri uri;
    Uri notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_m);

        //set relations by id
        name = (EditText) findViewById(R.id.editText1);
        push = (Button) findViewById(R.id.bPress);
        display = (TextView) findViewById(R.id.textView2);
        pi = (Button) findViewById(R.id.b2);
        uri = Uri.parse("https://mybu.bournemouth.ac.uk/webapps/portal/frameset.jsp");
        notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        push.setOnClickListener((OnClickListener) this);

        //
        push.setOnClickListener(new View.OnClickListener() {
            int i = 0;

            @Override
            public void onClick(View v) {
                if(i == 0){

                    if(!name.getText().toString().equals("")){
                        display.setText("Hey, " + name.getText().toString() + "!");
                        push.setText("reset");
                        i++;
                    }
                    else{
                        display.setText("Is Anybody there?");
                        try{
                            rTone = RingtoneManager.getRingtone(getApplicationContext(), notification);
                            rTone.play();
                        }catch(Exception e){}
                    }
                }
                else{

                    name.setText(null);
                    display.setText("");
                    push.setText("press me!");
                    i--;
                }
            }
        });
        pi.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.m, menu);
        return true;
    }

}

My logcat:

10-12 00:49:20.443: I/ApplicationPackageManager(12517): cscCountry is not German : BTU
10-12 00:49:21.193: W/dalvikvm(12517): threadid=1: thread exiting with uncaught exception (group=0x40018578)
10-12 00:49:21.240: E/AndroidRuntime(12517): FATAL EXCEPTION: main
10-12 00:49:21.240: E/AndroidRuntime(12517): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.example.task01/app.example.task01.MActivity}: java.lang.ClassCastException: app.example.task01.MActivity
10-12 00:49:21.240: E/AndroidRuntime(12517):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at android.os.Looper.loop(Looper.java:130)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at android.app.ActivityThread.main(ActivityThread.java:3687)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at java.lang.reflect.Method.invokeNative(Native Method)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at java.lang.reflect.Method.invoke(Method.java:507)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at dalvik.system.NativeStart.main(Native Method)
10-12 00:49:21.240: E/AndroidRuntime(12517): Caused by: java.lang.ClassCastException: app.example.task01.MActivity
10-12 00:49:21.240: E/AndroidRuntime(12517):    at app.example.task01.MActivity.onCreate(MActivity.java:41)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-12 00:49:21.240: E/AndroidRuntime(12517):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-12 00:49:21.240: E/AndroidRuntime(12517):    ... 11 more

Without seeing your LogCat, I'm guessing your problem is that you aren't overriding the onClick method here:

pi.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });

Do:

pi.setOnClickListener(new View.OnClickListener() {
            @Override <--- notice the override annotation
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
    });

According your logcat:

Caused by: java.lang.ClassCastException: app.example.task01.MActivity
   at app.example.task01.MActivity.onCreate(MActivity.java:41)

Try to delete:

push.setOnClickListener((OnClickListener) this);

in your MActivity. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM