添加第二个按钮后Android应用程序崩溃

时间:2013-10-11 20:42:42

标签: java android

由于某些原因我无法解决,当我添加第二个按钮和onClick事件“pi”时,我的应用程序将无法运行。我可能只是有一个沉闷的时刻,并应该创建一个'onClick'方法与内部两个。任何帮助非常感谢。

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

}

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

2 个答案:

答案 0 :(得分:0)

没有看到你的LogCat,我猜你的问题是你没有在这里覆盖onClick方法:

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

执行:

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

答案 1 :(得分:0)

根据你的logcat:

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

尝试删除:

push.setOnClickListener((OnClickListener) this);

在您的MActivity中。 :)