Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android:尝试设置按钮的任何属性时出现空指针异常_Java_Android - Fatal编程技术网

Java android:尝试设置按钮的任何属性时出现空指针异常

Java android:尝试设置按钮的任何属性时出现空指针异常,java,android,Java,Android,我已经检查了这里面的所有内容,但是仍然得到一个空指针异常。每当我试图更改java文件中按钮的属性时,应用程序停止工作,logcat显示空指针异常。请帮帮我 这是我的密码 package com.example.rapid_finger; import java.util.Random; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity;

我已经检查了这里面的所有内容,但是仍然得到一个空指针异常。每当我试图更改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>

您在
设置contentview
之前已经找到了
查看视图

setContentView
应该放在第一位

@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\u play\u屏幕)
after
super.onCreate(savedInstanceState)

// 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();
}