0

何らかの理由で、2 番目のボタンと onClick イベント「pi」を追加すると、アプリケーションが実行されません。私は退屈な瞬間を過ごしているだけかもしれません.おそらく2つの内部で「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
4

2 に答える 2

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で。:)

于 2013-10-12T01:52:34.570 に答える