我一直试图寻找答案,但我唯一理解的是我的代码第22行出了点问题,但我不明白为什么。我确定它有点蠢,但我昨天开始编写android代码,请原谅我!
我的代码是:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class Benvenuto extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_benvenuto);
}
public void entra(View view) {
Intent entra1 = new Intent(this, domande1.class);
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId()) {
case R.id.condizioni:
if (checked)
startActivity(entra1);
else
Toast.makeText(this, "Devi approvare termini e condizioni prima di procedere!", Toast.LENGTH_SHORT).show();
break;
}
}
}
因此,基本上,用户必须同意条款和条件。如果他同意,那么他会跳到下一个活动。如果没有,就会出现祝酒词!
但是当按下按钮时应用程序崩溃(在添加复选框之前,它工作正常)。
我得到的错误是
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.suspicio.turboscuse, PID: 547
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21209)
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:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21209)
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:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.CheckBox
at com.suspicio.turboscuse.Benvenuto.entra(Benvenuto.java:22)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21209)
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:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
你能帮我吗?
答案 0 :(得分:1)
问题是view
变量不是CheckBox
,它似乎是AppCompatButton
。
您可以将支票更改为以下内容:
if(view instanceof CheckBox){
// The checkbox was pressed
if(((CheckBox) view).isChecked()){
// The checkbox is checked
startActivity(entra1);
}else{
// The checkbox is not checked
Toast.makeText(this, "Devi approvare termini e condizioni prima di procedere!", Toast.LENGTH_SHORT).show();
}
}else{
// The button was pressed
}
答案 1 :(得分:0)
试试这个:
import java.util.Scanner;
public class DataAnalyze {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i, j, k, sampleSum = 0; /* the variables that will be used */
int minimum = Integer.MAX_VALUE, maximum = Integer.MIN_VALUE; /* This will be used to find min and max */
int[] trialAvg = new int [5];
System.out.println("Please enter the sample size: ");
int sampleSize = input.nextInt();
long [][] arr = new long[5][sampleSize+1]; /* using long as it will be a larger size */
for(i = 0, k = 0; i < 4; sampleSum = 0, i++, k = 0) {
System.out.println(" Enter numbers for Trial: " + (i + 1));
/* asking user to input numbers for trials with for loop */
for (j = 0; j < sampleSize; j++, k++) {
System.out.println("Enter Sample # " + (k +1) + ":");
arr[i][j]= input.nextInt();
sampleSum += arr[i][j];
/* the sampSum will equal arr[i][j] */
}
trialAvg[i] = sampleSum/sampleSize;
System.out.println("");
/* finding average */
}
for (i = 0; i < 4; i++) {
if(maximum < trialAvg[i])
maximum = trialAvg[i];
/* testing to find min and max */
if(minimum > trialAvg[i])
minimum = trialAvg[i];
}
System.out.println("\tSample # \tTrial 1 \t\tTrial 2 \t\tTrial 3 \t\tTrial 4");
/* using \t to set it in table format and save it */
for ( i = 0; sampleSize < 4; i++) {
System.out.println("\t " + (i + 1) + "\t\t " + arr[0][i] + "\t\t " + arr[1][i] + "\t\t " + arr[2][i] + "\t\t " + arr[3][i]);
}
System.out.println("-----------------------------------------------------------------------------------");
System.out.println("Average:\t\t " + trialAvg[0] + "\t\t " + trialAvg[1] + "\t\t " + trialAvg[2] + "\t\t " + trialAvg[3]);
System.out.println("Minimum Average: " + minimum);
System.out.println("Maximum Average: " + maximum);
System.out.println("");
if(maximum < (minimum*2))
System.out.println("The trials concur with each other!");
else if (maximum == minimum)
System.out.println("The trials match EXACTLY!");
else
System.out.println("The trials do NOT concur!");
input.close();
}
}
在xml中:
public void itemClicked(View v) {
CheckBox checkBox = (CheckBox) v;
if(checkBox.isChecked())
startActivity(entra1);
else
Toast.makeText(this, "Devi approvare termini e condizioni prima di procedere!", Toast.LENGTH_SHORT).show();
}