2017-01-26 8 views
-2

を使用している間、私は動的にボタンの色を変更しようとしているので、私はそれを行うためのスレッドを作成したが、次のエラーがnullオブジェクト参照エラースレッド

W/System.errの来ている:Javaの。 lang.NullPointerException: nullオブジェクト参照

上 仮想メソッド '空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を入力すると、このエラーの理由とどのようにそれを解決するために何であるか、nullになっB4しまいましたか?

+1

あなたのXMLコードも入力してください – W4R10CK

+0

@MikeM。これを解決する方法 – mesmerizer

答えて

0

MainActivityのオブジェクトが2つあります。最初にあなたのアプリはonCreate()から始まり、あなたのフィールドは初期化されます。次にnew Thread(new MainActivity())と呼びます。これは、MainActivityの新しいオブジェクトを作成することを意味します。その後が初期化されていないスレッドのボタンにアクセスしますnew MainActivityを呼び出すとonCreate()が呼び出されないためです。

1

基本的にMainActivityはUIスレッドで実行されており、すべてのボタンはUIスレッドでアクセス可能です。新しいスレッドを開始し、ボタンのプロパティを変更しようとしているので、ボタンにアクセスできません。我々は任意のUI要素を変更するのであれば、私たちは

someActivity.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      // Code here will run in UI thread 
     } 
}); 

または

new Handler().post(new Runnable() { 
    @Override 
    public void run() { 
     // Code here will run in UI thread 
    } 
}); 
目で

更新UI要素()以下のように、ハンドラまたはrunOnUiThreadを使用してそれをしなければなりません。

関連する問題