我使用多个OnClickListener获得致命异常

时间:2013-03-18 12:03:38

标签: java android fatal-error

主要的事情发生在FATAL EXECEPTION我的应用程序没有启动和错误来到不幸停止它一直崩溃 尝试切换案例,否则如果仍然错误继续应用程序不断崩溃 谷歌搜索和搜索堆栈流不同的用户堆栈流面临这个错误仍然错误继续我甚至在尝试他们的解决方案与单个if和单个onclicklistener应用程序设法运行但不与多个onlicklistener是否有不同的方式来运行多个onclick监听器应用程序请帮助我

这是MainActivity.java

package com.example.cricscore;

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log;
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

// Private member field to keep track of the count 
private int mCount = 0; 
private static final String TAG = "MainActivity"; 


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    Button buttonA = (Button) findViewById(R.id.button1);    
    buttonA.setOnClickListener((OnClickListener) this);
    Button buttonB = (Button) findViewById(R.id.button2); 
    buttonB.setOnClickListener((OnClickListener) this);
 }

   public void onClick(View v) {
       try {
        switch (v.getId()) {
           case R.id.button1: 
               mCount++; 
               final TextView countTextView = (TextView) findViewById(R.id.textView1);
               countTextView.setText(""+ mCount); 
            break;
           case R.id.button2:
               mCount= mCount+2 ; 
               final TextView countTextView1 = (TextView) findViewById(R.id.textView1);
               countTextView1.setText(""+ mCount);
            break;


        }
       }
       catch(Throwable t) {
           Log.e(TAG, t.getMessage(), t);

       }

   }
 }

Activity_main.xml是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/button2"
    android:layout_below="@+id/textView1"
    android:text="Runs" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignRight="@+id/textView2"
    android:layout_marginRight="19dp"
    android:text="Wickets" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_alignParentRight="true"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView3"
    android:text="1" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_toRightOf="@+id/button1"
    android:text="2" />

<Button
    android:id="@+id/button4"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1"
    android:text="6" />

<Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button5"
    android:layout_alignRight="@+id/textView4"
    android:text="Wicket" />

<Button
    android:id="@+id/button7"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button8"
    android:layout_alignBottom="@+id/button8"
    android:layout_alignLeft="@+id/button5"
    android:text="Balls" />

<Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView3"
    android:layout_toRightOf="@+id/button7"
    android:text="4" />

<Button
    android:id="@+id/button8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="44dp"
    android:layout_toLeftOf="@+id/textView2"
    android:text="Button" />

<Button
    android:id="@+id/button5"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button4"
    android:layout_alignBottom="@+id/button4"
    android:layout_toLeftOf="@+id/textView7"
    android:text="No/Wide" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView7"
    android:layout_alignBottom="@+id/textView7"
    android:layout_alignRight="@+id/button5"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button8"
    android:layout_toLeftOf="@+id/textView4"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

</RelativeLayout>

logcat的

03-18 14:09:37.717: E/AndroidRuntime(360): FATAL EXCEPTION: main
03-18 14:09:37.717: E/AndroidRuntime(360): java.lang.RuntimeException: Unable to start      
    activity
  ComponentInfo{com.example.abc/com.example.abc.MainActivity}:  java.lang.ClassCastException:    
  com.example.abc.MainActivity
  03-18 14:09:37.717: E/AndroidRuntime(360):    at     
  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at   
  android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1663)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at android.app.ActivityThread.access$1500
  (ActivityThread.java:117)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at android.app.ActivityThread$H.handleMessage 
  (ActivityThread.java:931)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at android.os.Handler.dispatchMessage 
  (Handler.java:99)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at android.os.Looper.loop(Looper.java:123)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at android.app.ActivityThread.main 
  (ActivityThread.java:3683)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at java.lang.reflect.Method.invokeNative(Native  
  Method)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at java.lang.reflect.Method.invoke 
  (Method.java:507)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at  
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at com.android.internal.os.ZygoteInit.main 
  (ZygoteInit.java:597)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at dalvik.system.NativeStart.main(Native Method)
  03-18 14:09:37.717: E/AndroidRuntime(360): Caused by: java.lang.ClassCastException:  
  com.example.abc.MainActivity
  03-18 14:09:37.717: E/AndroidRuntime(360):    at com.example.abc.MainActivity.onCreate 
  (MainActivity.java:30)
  03-18 14:09:37.717: E/AndroidRuntime(360):    at   
  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
   03-18 14:09:37.717: E/AndroidRuntime(360):   at  
   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
   03-18 14:09:37.717: E/AndroidRuntime(360):   ... 11 more

经过一些修改第一个回答告诉我的代码没有崩溃,但我只能看到android_mainfest仍然相同的空白活动

package com.example.cricscore;

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log;
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity implements OnClickListener { 

// Private member field to keep track of the count 
private int mCount = 0; 
/** Called when the activity is first created. 
 * @param OnClickListener */
public void onCreate(Bundle savedInstanceState, android.view.View.OnClickListener 
OnClickListener){
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    Button buttonA = (Button) findViewById(R.id.button1);    
    buttonA.setOnClickListener((OnClickListener) );
    Button buttonB = (Button) findViewById(R.id.button2); 
    buttonB.setOnClickListener((OnClickListener) );
 }

   public void OnClickListener(View v) {

        switch (v.getId()) {
           case R.id.button1: 
               mCount++; 
               final TextView countTextView = (TextView) findViewById(R.id.textView1);
               countTextView.setText(""+ mCount); 
            break;
           case R.id.button2:
               mCount= mCount+2 ; 
               final TextView countTextView1 = (TextView) findViewById(R.id.textView1);
               countTextView1.setText(""+ mCount);
            break;


        }


   }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}
 }

1 个答案:

答案 0 :(得分:1)

setOnClickListener方法需要View.OnClickListener类型的参数。你正在做的是接受Activity本身,并试图在这段代码中强制它是OnClickListener,而不是,{/ p>

Button buttonA = (Button) findViewById(R.id.button1);    
buttonA.setOnClickListener((OnClickListener) this); // <-- here
Button buttonB = (Button) findViewById(R.id.button2); 
buttonB.setOnClickListener((OnClickListener) this); // <-- and here too

您应该使用:

将您的活动声明为OnClickListener
public class MainActivity extends Activity implements OnClickListener

并且由于您已经有onClick(View v)方法,因此应该修复问题。