我的Android Studio模拟器(Nexus_7_API_21)中出现错误,我的应用程序在尝试运行时已停止工作。我启动了LogCat,我认为错误是由于空指针引用,但我不知道问题出在哪里。
这是我的.java主文件。
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
public class MainActivity extends ActionBarActivity
{
RelativeLayout bg;
RadioButton r1;
RadioButton r2;
RadioButton r3;
RadioButton r4;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bg = (RelativeLayout) findViewById(R.id.back);
r1 = (RadioButton) findViewById(R.id.rButton1);
r2 = (RadioButton) findViewById(R.id.rButton2);
r1 = (RadioButton) findViewById(R.id.rButton3);
r1 = (RadioButton) findViewById(R.id.rButton4);
r1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
return;
}
});
r2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
return;
}
});
r3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
return;
}
});
r4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
return;
}
});
}
@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);
}
}
这是我的XML文件(activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/back"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#210d0a0d">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="200dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/welcomeString"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="180dp"
android:layout_height="150dp" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rButton1"
android:id="@+id/rButton1"
android:layout_gravity="center_vertical"
android:checked="false" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rButton2"
android:id="@+id/rButton2"
android:layout_gravity="center_vertical" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rButton3"
android:id="@+id/rButton3"
android:layout_gravity="center_vertical" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rButton4"
android:id="@+id/rButton4"
android:layout_gravity="center_vertical" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:textSize="25dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:3)
r1 = (RadioButton) findViewById(R.id.rButton3);
r1 = (RadioButton) findViewById(R.id.rButton4);
是个问题。您使用的是r1而不是r3和r4。
r3 = (RadioButton) findViewById(R.id.rButton3);
r4 = (RadioButton) findViewById(R.id.rButton4);