我試圖動態地更改按鈕的顏色,所以我創建了一個線程來執行,但下面的錯誤來了空對象引用錯誤,同時使用一個線程
W/System.err的:JAVA。 lang.NullPointerException:嘗試調用 虛擬方法 '無效android.widget.Button.setBackgroundColor(INT)' 上 空對象引用
下面是我的代碼
public class MainActivity extends AppCompatActivity implements View.OnClickListener,Runnable {
Button b1, b2, b3, b4;
Random r = new Random();
int random_selection;
Thread t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b1.setBackgroundColor(Color.BLUE);
b2.setBackgroundColor(Color.BLUE);
b3.setBackgroundColor(Color.BLUE);
b4.setBackgroundColor(Color.BLUE);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
delay(7000);
main();
}
private void main() {
// Thread t1 = new Thread(new MainActivity());
// this wi run() function
t1 = new Thread(new MainActivity());
System.out.println("inside main");
t1.start();
System.out.println("after t1.start()");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
break;
case R.id.button2:
break;
case R.id.button3:
break;
case R.id.button4:
break;
default:
android.widget.Toast.makeText(getBaseContext(), "Wrong Selection",
android.widget.Toast.LENGTH_SHORT).show();
break;
}
}
public int randomgen() {
random_selection = r.nextInt(4) + 1;
return random_selection;
}
public void delay(int t) {
try {
Thread.sleep(t);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
//@Override•
public void run() {
try {
System.out.println("inside run");
int x, count = 0;
for (; ;) {
count = count + 1;
x = randomgen();
// android.widget.Toast.makeText(getBaseContext(), x,android.widget.Toast.LENGTH_SHORT).show();
if (x == 1) {
b1.setBackgroundColor(Color.BLACK);
delay(5000);
b1.setBackgroundResource(android.R.drawable.btn_default);
System.out.println(this.b1);
} else if (x == 2) {
b2.setBackgroundColor(Color.BLACK);
delay(5000);
b2.setBackgroundResource(android.R.drawable.btn_default);
System.out.println(this.b2);
} else if (x == 3) {
b3.setBackgroundColor(Color.BLACK);
delay(5000);
b3.setBackgroundResource(android.R.drawable.btn_default);
System.out.println(this.b3);
} else if (x == 4) {
b4.setBackgroundColor(Color.BLACK);
delay(5000);
b4.setBackgroundResource(android.R.drawable.btn_default);
System.out.println(this.b4);
}
if (count == 4) {
break;
}
}
} catch (Exception e) {
System.out.println("inside catch");
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.hellotape.hdl.test_bomber.MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="51dp"
android:layout_marginTop="127dp"
android:id="@+id/button"
android:visibility="visible" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button"
android:layout_alignParentEnd="true"
android:layout_marginEnd="52dp"
android:id="@+id/button2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignEnd="@+id/button"
android:layout_marginTop="113dp"
android:id="@+id/button3" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button3"
android:layout_alignEnd="@+id/button2"
android:id="@+id/button4"
android:elevation="5dp" />
</RelativeLayout>
在調試我,只要我進入線程T1,按鈕變量B1,B2,B3得到了,B4變爲零,這是什麼錯誤的原因以及如何解決呢?
也添加您的xml代碼 – W4R10CK
@MikeM。如何解決這個問題 – mesmerizer