2016-03-14 59 views
0

當檢查清單被選中時,應用程序將停止併發生logcat錯誤。 我不知道爲什麼,但我會在下面包含代碼,如果您需要我包含更多,請隨時提問。當檢查清單被選中時,應用程序停止

03-14 06:42:09.980 29887-29887/xyz.ashraf.whoisdelasalle E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: xyz.ashraf.whoisdelasalle, PID: 29887 
                      java.lang.IllegalStateException: Could not find method onCheckboxClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.CheckBox with id 'respect' 
                       at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4485) 
                       at android.view.View$DeclaredOnClickListener.onClick(View.java:4449) 
                       at android.view.View.performClick(View.java:5204) 
                       at android.widget.CompoundButton.performClick(CompoundButton.java:122) 
                       at android.view.View$PerformClick.run(View.java:21153) 
                       at android.os.Handler.handleCallback(Handler.java:739) 
                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

^^日誌貓錯誤^^

package xyz.ashraf.whoisdelasalle; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 

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

     Button b = (Button) findViewById(R.id.who); 
     Button today = (Button) findViewById(R.id.today); 
     Button sallian = (Button) findViewById(R.id.sallian); 
     Button how = (Button) findViewById(R.id.toBe); 
     Button moreInfo = (Button) findViewById(R.id.info); 

     b.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, Pop.class)); 
      } 
     }); 

     today.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, Pop_today.class)); 
      } 
     }); 
     sallian.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, Pop_sallian.class)); 
      } 
     }); 
     how.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, Pop_how.class)); 
      } 
     }); 
     moreInfo.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, Pop_info.class)); 
      } 
     }); 
    } 
} 

^^主要活動^^

package xyz.ashraf.whoisdelasalle; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.view.View; 
import android.widget.Button; 

/** 
* Created by Ashraf on 1/27/2016. 
*/ 
public class Pop_sallian extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.popwindow_sallian); 

     DisplayMetrics dm = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(dm); 

     int width = dm.widthPixels; 
     int height = dm.heightPixels; 

     getWindow().setLayout((int)(width*.8),(int)(height*.6)); 

     Button checkButton = (Button) findViewById(R.id.check); 
     checkButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(Pop_sallian.this, check_Button.class)); 
      } 
     }); 
     Button okButton = (Button) findViewById(R.id.okButton_sallian); 
     okButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       finish(); 
      } 
     }); 
    } 
} 

^^代碼中調用checkbutton.java檢查按鈕是^^

package xyz.ashraf.whoisdelasalle; 

import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.TextView; 
import android.widget.CompoundButton.OnCheckedChangeListener; 

/** 
* Created by Ashraf on 3/2/2016. 
*/ 
public class check_Button extends Pop_sallian { 
    // Connects The variable to an xml id 


    TextView output = (TextView) findViewById(R.id.output); 

    //sets the variable to 0 
    int score = 0; 

    public void onCheckboxClicked(View view) { 
     boolean checked = ((CheckBox) view).isChecked(); 

     switch (view.getId()) { 
      case R.id.concern: 
       if (checked) { 
        score += 1; 
       } 
       break; 
      case R.id.faith: 
       if (checked) { 
        score += 1; 
       } 
       break; 
      case R.id.respect: 
       if (checked) { 
        score += 1; 
       } 
       break; 
      case R.id.education: 
       if (checked) { 
        score += 1; 
       } 
       break; 
      case R.id.community: 
       if (checked) { 
        score += 1; 
       } 
       break; 
     } 

     // adds the variables together to form a score 

     if (score == 0) { 
      output.setText("Come on! Get involved, your la sallian community needs you."); 
     } else if (score == 1) { 
      output.setText("Good start, keep going!"); 
     } else if (score == 2) { 
      output.setText("Room to improve but doing good!"); 
     } else if (score == 3) { 
      output.setText("Very good, others look up to you!"); 
     } else if (score == 4) { 
      output.setText("Wow, you really are an inspiration"); 
     } else if (score == 5) { 
      output.setText("Excellent! You're a leader in your la sallian community"); 
     } else { 
      output.setText("Unknown"); 
     } 
     // changes the output text based on score value 
    } 
} 

^^代碼檢查多少車cklist已檢查並增加了一個價值吧。^^

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ScrollView01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<RelativeLayout 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Are you a Sallian?" 
     android:id="@+id/textView7" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textSize="30sp" 
     android:textColor="#000000" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do you meet the following prerequisites, if you do you may be a Sallian" 
     android:id="@+id/textView8" 
     android:layout_below="@+id/textView7" 
     android:layout_centerHorizontal="true" 
     android:textSize="20sp" 
     android:textColor="#000000" /> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Are you concerened for the poor and Social Justice?" 
     android:id="@+id/concern" 
     android:textSize="18sp" 
     android:layout_below="@+id/textView8" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="10dp" 
     android:onClick="onCheckboxClicked"/> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do you have faith in the presence of God?" 
     android:id="@+id/faith" 
     android:textSize="15sp" 
     android:layout_below="@+id/concern" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="3dp" 
     android:onClick="onCheckboxClicked"/> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do you have Respect for all people?" 
     android:id="@+id/respect" 
     android:layout_below="@+id/faith" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="3dp" 
     android:textSize="15sp" 
     android:onClick="onCheckboxClicked"/> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do you value education?" 
     android:id="@+id/education" 
     android:textSize="15sp" 
     android:layout_below="@+id/respect" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="3dp" 
     android:onClick="onCheckboxClicked"/> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Are you inclusive in your community?" 
     android:id="@+id/community" 
     android:layout_below="@+id/education" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textSize="15sp" 
     android:checked="false" 
     android:onClick="onCheckboxClicked"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Ok" 
     android:id="@+id/okButton_sallian" 
     android:layout_below="@+id/community" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="90dp" 
     android:layout_marginBottom="20dp" 
     android:background="#FAFAFA" 
     android:textColor="#00E676" 
     android:elevation="2dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Check" 
     android:id="@+id/check" 
     android:textColor="#00E676" 
     android:elevation="2dp" 
     android:background="#FAFAFA" 
     android:layout_alignTop="@+id/okButton_sallian" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="20dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:id="@+id/output" 
     android:textColor="#1eff00" 
     android:textSize="20sp" 
     android:layout_below="@+id/community" 
     android:layout_centerHorizontal="true" 
     android:layout_above="@+id/check" 
     android:textIsSelectable="false" /> 
</RelativeLayout> 
    </ScrollView> 

^^ XML ^^

請包括代碼示例爲你的答案的一部分。

回答

0

在您的活動中,您沒有任何方法使用此名稱onCheckboxClicked。

以XML爲您添加的onClick方法見下文

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Are you concerened for the poor and Social Justice?" 
    android:id="@+id/concern" 
    android:textSize="18sp" 
    android:layout_below="@+id/textView8" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="10dp" 
    android:onClick="onCheckboxClicked"/> 

安卓的onClick = 「onCheckboxClicked」

檢查出所有複選框。

,你需要在你的activty添加一個方法類似這樣的

public void onCheckboxClicked(View v){ 
// do what ever you want. 
} 

但複選框沒有必要增加這樣的方法。您只需添加onCheckChangedListener

您的活動並檢查您的複選框的狀態。

在您的活動添加此的onCreate

CheckBox checkBox = (CheckBox) findViewById(R.id.concern); 

checkBox .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        if (isChecked) { 
         // your check box is checked now. do what ever you wnat 
        } else { 
         // you check box is uncheckd now do what ever you want. 
        } 
       } 
      }); 

做所有複選框相同。

如果想不想改變任何按鈕,複選框,單擊狀態

然後做以下事情。

獲取您活動中的所有複選框視圖。像我一樣關注cehck箱

和按鈕點擊內做到這一點

checkbox.setchecked(true) 

checkbox.setchecked(false) 

我希望你明白,現在我剛纔說的。

+0

我明白你在說什麼,但我在哪裏把它放在我的代碼中? –

+0

你想放什麼? checkchanged偵聽器或onCheckboxClicked方法? –

+0

如果你想添加監聽器,那麼你可以在你的onCreate中添加這個。如果你想添加方法,可以將它添加到onCreate範圍之外的任何地方。 –

0

在已宣佈的複選框xml文件:

android:onClick="onCheckboxClicked" 

這意味着,在您的活動需要聲明的方法:

public void onCheckboxClicked(View view){} 
+0

我在check_Button中聲明 –

+0

它必須在使用此佈局資源的活動中。 –

相關問題