我有“等待调试器附加”消息和一个空白活动,无论什么时候我都没有发生任何事情!我正在尝试做一个显示问题的简单主要活动,“+”是真的而“ - ”是假的。就像真假游戏一样。
这是我的manigest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calculator"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission>
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:debuggable="true"
>
<activity
android:name="com.example.calculator.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这是我的java:
package com.example.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements android.view.View.OnClickListener {
Button button;
Button button2;
Button button3;
TextView text;
String[] questions ;
boolean[] answers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
text = (TextView) findViewById(R.id.textView1) ;
questions[0]="Are u hungry?";
answers[0]=true;
text.setText(questions[0]);
button.setOnClickListener(this);
button2.setOnClickListener(this);
}
public void onClick(View v) {
//parsing should be here
switch(v.getId()){
case R.id.button1:
if( answers[0]==true){
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("True !Congratulations");
alertDialog.show();}
else{ AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("false !You are idiot!");
alertDialog.show();}
break;
case R.id.button2:
if( answers[0]==false){
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("Correct !Congratulations");
alertDialog.show();}
else{ AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("Incorrect !You are idiot!");
alertDialog.show();}
break;
}
}
}
这是我的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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#99FFFF">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:text="@string/num1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#FFFFFF"/>
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/button1"
android:text="@string/minus"
android:background="#FFFFFF"/>
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:background="#FFFFFF"
android:text="@string/add" />
</RelativeLayout>
答案 0 :(得分:0)
您将变量声明为数组
String[] questions ;
boolean[] answers;
然后你在没有初始化的情况下使用它们,所以java不知道数组的大小。
questions[0]="Are u hungry?";
answers[0]=true;
一种可能的解决方案是定义数组的大小:
String[] questions = new String[10]; // define the size of the array
另一个解决方案是使用ArrayList。