我的基本Q& A应用程序无法运行

时间:2015-11-02 10:10:37

标签: java android

我制作了一个基本的Q& A应用程序,当按下按钮时,它会接收答案并在小文本字段上产生输出。但是我一直在我的模拟器上收到错误消息"不幸的是App停止了#34;。

这是java:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

public int score = 0; //answer score
final  TextView Score = (TextView) findViewById(R.id.Score);
Button b = (Button)findViewById(R.id.button); //button


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    question1(); //call params for question

    //button listener, when button clicked, produce output on textfield "Score"
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Score.setText(String.valueOf(score));
        }

    });

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void question1(){
    String Question1 = "Question 1";

    TextView t = (TextView) findViewById(R.id.Question);
    TextView A = (TextView) findViewById(R.id.AnswerA);
    TextView B = (TextView) findViewById(R.id.AnswerB);
    TextView C = (TextView) findViewById(R.id.AnswerC);
    TextView D = (TextView) findViewById(R.id.AnswerD);

    Score.setText(String.valueOf(score));

    t.setText("Question 1");
    A.setText("Answer A");
    B.setText("Answer B");
    C.setText("Answer C");
    D.setText("Answer D");

}

public int onCheckboxClickedA(){
    return score = score + 1;
}
public int onCheckboxClickedB(){
    return score = score + 2;
}
public int onCheckboxClickedC(){
    return score = score + 3;
}
public int onCheckboxClickedD(){
    return score = score + 4;
}

}

这是XML:     

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/Question"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/AnswerA"
    android:layout_below="@+id/Question"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="58dp"
    android:checked="false"
    android:layout_toLeftOf="@+id/Question"
    android:layout_toStartOf="@+id/Question"
    android:onClick="onCheckboxClickedA"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/AnswerB"
    android:checked="false"
    android:layout_below="@+id/AnswerA"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="onCheckboxClickedB"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New CheckBox"
    android:id="@+id/AnswerC"
    android:layout_below="@+id/AnswerB"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false"
    android:onClick="onCheckboxClickedC"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New CheckBox"
    android:id="@+id/AnswerD"
    android:layout_below="@+id/AnswerC"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false"
    android:onClick="onCheckboxClickedD"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/AnswerD"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="78dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/Score"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="54dp" />

2 个答案:

答案 0 :(得分:1)

将此行的位置更改为onCreate()方法,

final  TextView Score = (TextView) findViewById(R.id.Score);
Button b = (Button)findViewById(R.id.button); //button

将上述行移至onCreate()这样的方法,

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

    final  TextView Score = (TextView) findViewById(R.id.Score);
    Button b = (Button)findViewById(R.id.button); //button

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    ...

答案 1 :(得分:1)

移动您的代码

TextView Score = (TextView) findViewById(R.id.Score);
Button b = (Button)findViewById(R.id.button); //button
onCreate(...)

之后的setContentView(R.layout.activity_main);