所以基本上,我有一个许可证测验android应用程序。在我检查了所有问题或仅回答了几个问题之后,按了提交按钮,而不是向我扔出我做了多少程序的消息,只是在没有警告或任何警告的情况下关闭。短暂的几秒钟后,它告诉我应用程序本身一直关闭。
public class CourseListAdapter extends
RecyclerView.Adapter<CourseListAdapter.MyHolder> {
private static final String ICON_PREFIX = "img_";
ArrayList<Example2> data2;
public ArrayList<CourseItem> data;
RecyclerView r;
Activity c;
Context context;
CourseListAdapterListener listener;
public void updateList(ArrayList<Example2> exampleList) {
this.data2 = exampleList;
}
public void updateList2(ArrayList<CourseItem> courseList) {
this.data = courseList;
}
public interface CourseListAdapterListener {
void onBook(int courseIndex);
}
public CourseListAdapter(ArrayList<CourseItem> data, RecyclerView r,
ArrayList<Example2> data2, Activity c) {
this.data2 = data2;
this.data = data;
this.r = r;
this.c = c;
}
public void setListener(CourseListAdapterListener listener) {
this.listener = listener;
}
@Override
public CourseListAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
LayoutInflater inflater = (LayoutInflater)
c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.custom_course_list, parent, false);
final MyHolder holder = new MyHolder(v);
holder.btnDetail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = holder.getAdapterPosition();
CourseItem item = data.get(pos);
Intent i = new Intent(view.getContext(),
DetailCourseActivity.class);
i.putExtra("course", item);
view.getContext().startActivity(i);
}
});
return holder;
}
@Override
public void onBindViewHolder(CourseListAdapter.MyHolder holder, int position)
{
if (holder instanceof CourseListAdapter.MyHolder) {
CourseListAdapter.MyHolder myHolder = (CourseListAdapter.MyHolder)
holder;
myHolder.textcourse.setText(data.get(position).course_name);
myHolder.tv_city_weather.setText(data.get(position).course_city);
myHolder.mTvWeatherTitle.setText(data.get(position).course_city);
myHolder.mTvWeatherTitle.setTypeface(Typeface.DEFAULT_BOLD);
myHolder.mBtnWeather.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
android.app.FragmentManager fm = c.getFragmentManager();
DialogWeather dialogFragment = new DialogWeather();
Bundle bundle = new Bundle();
bundle.putString("message", data.get(position).course_city);
dialogFragment.setArguments(bundle);
dialogFragment.show(fm, "sam");
}
});
Glide.with(c)
.load(data.get(position).course_image)
.into(holder.image);
if (position == 0 || position == 1) {
holder.btnBook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onBook(holder.getAdapterPosition());
}
});
}
}
}
@Override
public int getItemCount() {
return data.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView textcourse, mTvWeatherTitle, tv_city_weather;
ImageView iconWeatherAdapter;
Button btnBook;
Button btnDetail;
LinearLayout mBtnWeather;
public MyHolder(View itemView) {
super(itemView);
iconWeatherAdapter =
itemView.findViewById(R.id.icon_weather_adapter);
image = (ImageView) itemView.findViewById(R.id.imgcourse);
textcourse = (TextView) itemView.findViewById(R.id.txtcoursename);
tv_city_weather = (TextView)
itemView.findViewById(R.id.tv_city_weather);
btnBook = (Button) itemView.findViewById(R.id.btnbook);
btnDetail = (Button) itemView.findViewById(R.id.btndetail);
mBtnWeather = itemView.findViewById(R.id.btn_weather);
mTvWeatherTitle = itemView.findViewById(R.id.tv_weather_title);
}
}
}
记录错误:
Import :
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.Toast;
Main activity :
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
// Question 1
CheckBox question1_choice1;
CheckBox question1_choice2;
CheckBox question1_choice3;
CheckBox question1_choice4;
CheckBox question1_choice5;
CheckBox question1_choice6;
// Question 2
CheckBox question2_choice1;
CheckBox question2_choice2;
CheckBox question2_choice3;
CheckBox question2_choice4;
// Question 3
CheckBox question3_choice1;
CheckBox question3_choice2;
CheckBox question3_choice3;
// Question 4
CheckBox question4_choice1;
CheckBox question4_choice2;
CheckBox question4_choice3;
CheckBox question4_choice4;
// Question 5
CheckBox question5_choice1;
CheckBox question5_choice2;
CheckBox question5_choice3;
CheckBox question5_choice4;
// Question 6
CheckBox question6_choice1;
CheckBox question6_choice2;
CheckBox question6_choice3;
// Question 7
CheckBox question7_choice1;
CheckBox question7_choice2;
CheckBox question7_choice3;
CheckBox question7_choice4;
// Question 8
CheckBox question8_choice1;
CheckBox question8_choice2;
CheckBox question8_choice3;
CheckBox question8_choice4;
// Question 9
CheckBox question9_choice1;
CheckBox question9_choice2;
CheckBox question9_choice3;
CheckBox question9_choice4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the keyboard
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.activity_main);
}
public void submitAnswers(View view) {
CharSequence resultsDisplay;
Log.e(LOG_TAG, " " + this.findViewById(R.id.question3_choice3));
int answer1_score;
int answer2_score;
int answer3_score;
int answer4_score;
int answer5_score;
int answer6_score;
int answer7_score;
int answer8_score;
int answer9_score;
int final_score;
//------------------------------------------------------------------------------------------
// Question 1 - Corect 1 - 4 - 5;
//------------------------------------------------------------------------------------------
Boolean answer1_choice1;
Boolean answer1_choice2;
Boolean answer1_choice3;
Boolean answer1_choice4;
Boolean answer1_choice5;
Boolean answer1_choice6;
question1_choice1 = (CheckBox) this.findViewById(R.id.question1_choice1);
question1_choice2 = (CheckBox) this.findViewById(R.id.question1_choice2);
question1_choice3 = (CheckBox) this.findViewById(R.id.question1_choice3);
question1_choice4 = (CheckBox) this.findViewById(R.id.question1_choice4);
question1_choice5 = (CheckBox) this.findViewById(R.id.question1_choice5);
question1_choice6 = (CheckBox) this.findViewById(R.id.question1_choice6);
answer1_choice1 = question1_choice1.isChecked();
answer1_choice2 = question1_choice2.isChecked();
answer1_choice3 = question1_choice3.isChecked();
answer1_choice4 = question1_choice4.isChecked();
answer1_choice5 = question1_choice5.isChecked();
answer1_choice6 = question1_choice6.isChecked();
if (answer1_choice1 && !answer1_choice2 && !answer1_choice3 && answer1_choice4 && answer1_choice5 && !answer1_choice6) {
answer1_score = 1;
} else {
answer1_score = 0;
}
//------------------------------------------------------------------------------------------
// Question 2 - Corect - 3
//------------------------------------------------------------------------------------------
Boolean answer2_choice1;
Boolean answer2_choice2;
Boolean answer2_choice3;
Boolean answer2_choice4;
question1_choice1 = (CheckBox) this.findViewById(R.id.question2_choice1);
question1_choice2 = (CheckBox) this.findViewById(R.id.question2_choice2);
question1_choice3 = (CheckBox) this.findViewById(R.id.question2_choice3);
question1_choice4 = (CheckBox) this.findViewById(R.id.question2_choice4);
answer2_choice1 = question2_choice1.isChecked();
answer2_choice2 = question2_choice2.isChecked();
answer2_choice3 = question2_choice3.isChecked();
answer2_choice4 = question2_choice4.isChecked();
if (!answer2_choice1 && !answer2_choice2 && answer2_choice3 && !answer2_choice4) {
answer2_score = 1;
} else {
answer2_score = 0;
}
//------------------------------------------------------------------------------------------
// Question 3 - Corect 2
//------------------------------------------------------------------------------------------
Boolean answer3_choice1;
Boolean answer3_choice2;
Boolean answer3_choice3;
question3_choice1 = (CheckBox) this.findViewById(R.id.question3_choice1);
question2_choice2 = (CheckBox) this.findViewById(R.id.question3_choice2);
question3_choice3 = (CheckBox) this.findViewById(R.id.question3_choice3);
answer3_choice1 = question3_choice1.isChecked();
answer3_choice2 = question3_choice2.isChecked();
answer3_choice3 = question3_choice3.isChecked();
if (!answer3_choice1 && answer3_choice2 && !answer3_choice3) {
answer3_score = 1;
} else {
answer3_score = 0;
}
//------------------------------------------------------------------------------------------
// Question 4 - Corect - 4
//------------------------------------------------------------------------------------------
Boolean answer4_choice1;
Boolean answer4_choice2;
Boolean answer4_choice3;
Boolean answer4_choice4;
question4_choice1 = (CheckBox) this.findViewById(R.id.question4_choice1);
question4_choice2 = (CheckBox) this.findViewById(R.id.question4_choice2);
question4_choice3 = (CheckBox) this.findViewById(R.id.question4_choice3);
question4_choice4 = (CheckBox) this.findViewById(R.id.question4_choice4);
answer4_choice1 = question4_choice1.isChecked();
answer4_choice2 = question4_choice2.isChecked();
answer4_choice3 = question4_choice3.isChecked();
answer4_choice4 = question4_choice4.isChecked();
if (!answer4_choice1 && !answer4_choice2 && !answer4_choice3 && answer4_choice4) {
answer4_score = 1;
} else {
answer4_score = 0;
}
//------------------------------------------------------------------------------------------
// Question 5 - Corect - 4
//------------------------------------------------------------------------------------------
Boolean answer5_choice1;
Boolean answer5_choice2;
Boolean answer5_choice3;
Boolean answer5_choice4;
question5_choice1 = (CheckBox) this.findViewById(R.id.question5_choice1);
question5_choice2 = (CheckBox) this.findViewById(R.id.question5_choice2);
question5_choice3 = (CheckBox) this.findViewById(R.id.question5_choice3);
question5_choice4 = (CheckBox) this.findViewById(R.id.question5_choice4);
answer5_choice1 = question5_choice1.isChecked();
answer5_choice2 = question5_choice2.isChecked();
answer5_choice3 = question5_choice3.isChecked();
answer5_choice4 = question5_choice4.isChecked();
if (!answer5_choice1 && !answer5_choice2 && !answer5_choice3 && answer5_choice4) {
answer5_score = 1;
} else {
answer5_score = 0;
}
//------------------------------------------------------------------------------------------
// Question 6 - Corect 3
//------------------------------------------------------------------------------------------
Boolean answer6_choice1;
Boolean answer6_choice2;
Boolean answer6_choice3;
question6_choice1 = (CheckBox) this.findViewById(R.id.question6_choice1);
question6_choice2 = (CheckBox) this.findViewById(R.id.question6_choice2);
question6_choice3 = (CheckBox) this.findViewById(R.id.question6_choice3);
answer6_choice1 = question6_choice1.isChecked();
answer6_choice2 = question6_choice2.isChecked();
answer6_choice3 = question6_choice3.isChecked();
if (!answer6_choice1 && answer6_choice2 && answer6_choice3) {
answer6_score = 1;
} else {
answer6_score = 0;
}
//------------------------------------------------------------------------------------------
// Question 7 - Corect 2 - 4
//------------------------------------------------------------------------------------------
Boolean answer7_choice1;
Boolean answer7_choice2;
Boolean answer7_choice3;
Boolean answer7_choice4;
question7_choice1 = (CheckBox) this.findViewById(R.id.question7_choice1);
question7_choice2 = (CheckBox) this.findViewById(R.id.question7_choice2);
question7_choice3 = (CheckBox) this.findViewById(R.id.question7_choice3);
question7_choice4 = (CheckBox) this.findViewById(R.id.question7_choice4);
answer7_choice1 = question7_choice1.isChecked();
answer7_choice2 = question7_choice2.isChecked();
answer7_choice3 = question7_choice3.isChecked();
answer7_choice4 = question7_choice4.isChecked();
if (!answer7_choice1 && answer7_choice2 && !answer7_choice3 && answer7_choice4) {
answer7_score = 1;
} else {
answer7_score = 0;
}
//------------------------------------------------------------------------------------------
// Question 8 - Correct Answer is "Wrist"
//------------------------------------------------------------------------------------------
Boolean answer8_choice1;
Boolean answer8_choice2;
Boolean answer8_choice3;
Boolean answer8_choice4;
question8_choice1 = (CheckBox) this.findViewById(R.id.question8_choice1);
question8_choice2 = (CheckBox) this.findViewById(R.id.question8_choice2);
question8_choice3 = (CheckBox) this.findViewById(R.id.question8_choice3);
question8_choice4 = (CheckBox) this.findViewById(R.id.question8_choice4);
answer8_choice1 = question8_choice1.isChecked();
answer8_choice2 = question8_choice2.isChecked();
answer8_choice3 = question8_choice3.isChecked();
answer8_choice4 = question8_choice4.isChecked();
if (!answer8_choice1 && !answer8_choice2 && answer8_choice3 && !answer8_choice4) {
answer8_score = 1;
} else {
answer8_score = 0;
}
//------------------------------------------------------------------------------------------
// Question 9 - Correct Answers is #2 "Stalagmites"
//------------------------------------------------------------------------------------------
Boolean answer9_choice1;
Boolean answer9_choice2;
Boolean answer9_choice3;
Boolean answer9_choice4;
question9_choice1 = (CheckBox) this.findViewById(R.id.question9_choice1);
question9_choice2 = (CheckBox) this.findViewById(R.id.question9_choice2);
question9_choice3 = (CheckBox) this.findViewById(R.id.question9_choice3);
question9_choice4 = (CheckBox) this.findViewById(R.id.question9_choice4);
answer9_choice1 = question9_choice1.isChecked();
answer9_choice2 = question9_choice2.isChecked();
answer9_choice3 = question9_choice3.isChecked();
answer9_choice4 = question9_choice4.isChecked();
if (!answer9_choice1 && !answer9_choice2 && !answer9_choice3 && answer9_choice4) {
answer9_score = 1;
} else {
answer9_score = 0;
}
//------------------------------------------------------------------------------------------
// Final Score
//------------------------------------------------------------------------------------------
final_score = answer1_score + answer2_score + answer3_score + answer4_score + answer5_score +
answer6_score + answer7_score + answer8_score + answer9_score;
if (final_score == 9) {
resultsDisplay = "Perfect! Ai rezolvat 9 din 9";
} else {
resultsDisplay = "Ai rezolvat " + final_score + " din 9";
}
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, resultsDisplay, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
Main activity.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="rocks.athrow.android_quiz_app.MainActivity">
<LinearLayout
android:id="@+id/quiz_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/question1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:text="@string/question1"
android:textColor="@color/primaryText"
android:textSize="18sp" />
<CheckBox
android:id="@+id/question1_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question1_choice1" />
<CheckBox
android:id="@+id/question1_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question1_choice2" />
<CheckBox
android:id="@+id/question1_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question1_choice3" />
<CheckBox
android:id="@+id/question1_choice4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question1_choice4" />
<CheckBox
android:id="@+id/question1_choice5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question1_choice5" />
<CheckBox
android:id="@+id/question1_choice6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question1_choice6" />
<TextView
android:id="@+id/question2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:text="@string/question2"
android:textColor="@color/primaryText"
android:textSize="18sp" />
<CheckBox
android:id="@+id/question2_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question2_choice1" />
<CheckBox
android:id="@+id/question2_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question2_choice2" />
<CheckBox
android:id="@+id/question2_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question2_choice3" />
<CheckBox
android:id="@+id/question2_choice4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question2_choice4" />
<TextView
android:id="@+id/question3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:text="@string/question3"
android:textColor="@color/primaryText"
android:textSize="18sp" />
<CheckBox
android:id="@+id/question3_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question3_choice1" />
<CheckBox
android:id="@+id/question3_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question3_choice2" />
<CheckBox
android:id="@+id/question3_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question3_choice3" />
<TextView
android:id="@+id/question4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:text="@string/question4"
android:textColor="@color/primaryText"
android:textSize="18sp" />
<CheckBox
android:id="@+id/question4_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question4_choice1" />
<CheckBox
android:id="@+id/question4_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question4_choice2" />
<CheckBox
android:id="@+id/question4_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question4_choice3" />
<CheckBox
android:id="@+id/question4_choice4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question4_choice4" />
<TextView
android:id="@+id/question5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:text="@string/question5"
android:textColor="@color/primaryText"
android:textSize="18sp" />
<CheckBox
android:id="@+id/question5_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question5_choice1" />
<CheckBox
android:id="@+id/question5_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question5_choice2" />
<CheckBox
android:id="@+id/question5_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question5_choice3" />
<CheckBox
android:id="@+id/question5_choice4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question5_choice4" />
<TextView
android:id="@+id/question6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:text="@string/question6"
android:textColor="@color/primaryText"
android:textSize="18sp" />
<CheckBox
android:id="@+id/question6_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question6_choice1" />
<CheckBox
android:id="@+id/question6_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question6_choice2" />
<CheckBox
android:id="@+id/question6_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question6_choice3" />