-3

我已经检查了这一切,但我仍然得到一个空指针异常。每当我尝试更改 java 文件中按钮的属性时,应用程序停止工作并且 logcat 显示空指针异常。请帮帮我这是我的代码

package com.example.rapid_finger;
import java.util.Random;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import  android.widget.Button;


public class PlayScreen extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Random rand = new Random();
    int data = rand.nextInt(99);
    String str = Integer.toString(data);
    Button b = (Button) findViewById(R.id.b1);
    b.setText(str);
    setContentView(R.layout.activity_play_screen);
    // Show the Up button in the action bar.
    setupActionBar();
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-    vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

这是我的 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=".PlayScreen" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/scores" />
<Button 
 android:id="@+id/b1"
 android:background="@drawable/back"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>

</RelativeLayout>
4

3 回答 3

4

findViewById之前做过setContentView

setContentView应该是第一位的

于 2013-09-18T07:22:30.887 回答
0
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play_screen);
    Random rand = new Random();
    int data = rand.nextInt(99);
    String str = Integer.toString(data);
    Button b = (Button) findViewById(R.id.b1);
    b.setText(str);
    // Show the Up button in the action bar.
    setupActionBar();
}

放在 setContentView(R.layout.activity_play_screen);后面super.onCreate(savedInstanceState);

于 2013-09-18T07:24:19.883 回答
0
// replace this code
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play_screen);
    Random rand = new Random();
    int data = rand.nextInt(99);
    String str = Integer.toString(data);
    Button b = (Button) findViewById(R.id.b1);
    b.setText(str);

    // Show the Up button in the action bar.
    setupActionBar();
}
于 2013-09-18T07:24:25.147 回答