提问人:Cristian Navarrete 提问时间:8/25/2015 最后编辑:ParkerCristian Navarrete 更新时间:8/25/2015 访问量:189
尝试在Android Studio中运行程序,但收到错误,我的应用程序已停止工作
Trying to run program in Android Studio but get error that my app has stopped working
问:
每当我尝试运行我的应用程序时,我都会在我的 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>
答:
3赞
Kashyap Kansara
8/25/2015
#1
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);
评论