2015-04-02 67 views
1

我是新來編程,我試圖設計一個簡單的數學遊戲.. 因爲我想有一個方法,它將文本設置爲textview後3秒,如果有任何按鈕被點擊,定時器必須取消。我想這一點,但它不工作...任何幫助將不勝感激 SRY我不好變量命名,.setText()不適用於textview時使用計時器

public class MainActivity extends Activity implements OnClickListener { 
Button b1, b2, b3; 
ImageView i, i2, i3; 
TextView t1, t2, t3; 
String s1, s2, s3, s4, s5; 
Button bk; 
int q, m, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, n[]; 
Random r1 = new Random(); 
Random r0 = new Random(); 
Timer t = new Timer(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    b1 = (Button) findViewById(R.id.button1); 
    b2 = (Button) findViewById(R.id.button2); 
    b3 = (Button) findViewById(R.id.button3); 
    i = (ImageView) findViewById(R.id.imageView1); 
    t1 = (TextView) findViewById(R.id.textView1); 
    t2 = (TextView) findViewById(R.id.textView2); 
    t3 = (TextView) findViewById(R.id.textView3); 
    b1.setOnClickListener(this); 
    b2.setOnClickListener(this); 
    b3.setOnClickListener(this); 

    imp(); 

} 

public void imp() { 

    t.schedule(new TimerTask() { 
     public void run() { 
      t3.setText("times up"); 
      t3.setVisibility(View.VISIBLE); 
     } 
    }, 3000); 

    r2 = r1.nextInt(9) + 1; 
    r3 = r1.nextInt(9) + 1; 
    r4 = r2 + r3; 
    r5 = r2 - r3; 
    r6 = r2 * r3; 

    n = new int[] { r4, r5, r6 }; 

    doit(); 
    while (r9 == r11 || r9 == r13 || r11 == r13) { 
     doit(); 
    } 
    s1 = Integer.toString(r2); 
    s2 = Integer.toString(r3); 
    s3 = Integer.toString(r9); 
    s4 = Integer.toString(r11); 
    s5 = Integer.toString(r13); 
    t1.setText(s1); 
    t2.setText(s2); 
    b1.setText(s3); 
    b2.setText(s4); 
    b3.setText(s5); 

    int[] images = { R.drawable.plus, R.drawable.minus, R.drawable.multiply }; 

    Random random = new Random(); 
    m = images[random.nextInt(images.length)]; 
    i.setImageResource(m); 
    bk.setFocusable(true); 
    bk.requestFocus(); 
    bk.requestFocusFromTouch(); 
} 

public void doit() { 
    r9 = 0; 
    r11 = 0; 
    r13 = 0; 
    r8 = r1.nextInt(3) + 0; 
    r9 = n[r8]; 
    r10 = r1.nextInt(3) + 0; 
    r11 = n[r10]; 
    r12 = r1.nextInt(3) + 0; 
    r13 = n[r12]; 
    int[] buttons = { R.id.button1, R.id.button2, R.id.button3 }; 
    q = buttons[r0.nextInt(buttons.length)]; 
    bk = (Button) findViewById(q); 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.button1: 
     click(r9, b1); 
     break; 
    case R.id.button2: 
     click(r11, b2); 
     break; 
    case R.id.button3: 
     click(r13, b3); 
     break; 
    } 
} 
public void click(int k, Button b) { 
    if (m == R.drawable.plus && k == r4 || m == R.drawable.minus && k == r5 || m == R.drawable.multiply && k == r6) { 
     t.cancel(); 
     imp(); 

    } else { 
     t3.setText("FINISH"); 
     t3.setVisibility(View.VISIBLE); 
     b.setPressed(true); 
     b1.setClickable(false); 
     b2.setClickable(false); 
     b3.setClickable(false); 
     b.requestFocusFromTouch(); 

    } 
    } 
} 

和佈局是`

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center_horizontal|center_vertical" 
android:orientation="vertical" > 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<TextView 
android:textSize="30dp" 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="46dp" 
android:layout_weight="0.97" 
android:editable="false" 
android:text="Large Text" 
android:gravity="right" 
android:textAppearance="?android:attr/textAppearanceLarge" /> 

<ImageView 
android:layout_weight="1" 
android:id="@+id/imageView1" 
android:layout_width="67dp" 
android:layout_height="29dp" 
android:src="@drawable/plus" /> 

<TextView 
android:textSize="30dp" 
android:id="@+id/textView2" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:layout_weight="1.00" 
android:text="Large Text" 
android:gravity="left" 
android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 

<LinearLayout 
android:id="@+id/pooka" 
android:paddingTop="100dp" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 
<Button 
    android:layout_weight="1" 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Button" /> 
<Button 
android:layout_weight="1" 
android:id="@+id/button2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Button" /> 

<Button 
android:layout_weight="1" 
android:id="@+id/button3" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Button" /> 

</LinearLayout> 

<TextView 
android:id="@+id/textView3" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:paddingTop="50dp" 
android:text="Large Text" 
android:textAppearance="?android:attr/textAppearanceLarge" 
android:textSize="50sp" 
android:visibility="invisible"/> 

</LinearLayout> 

使得從答案更改後,現在我得到的onclick問題, 這裏我logcat的報告

`04-03 05:11:14.084: E/AndroidRuntime(9317): FATAL EXCEPTION: main 
04-03 05:11:14.084: E/AndroidRuntime(9317): java.lang.IllegalStateException: Timer was canceled 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at java.util.Timer.scheduleImpl(Timer.java:561) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at java.util.Timer.schedule(Timer.java:459) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at com.example.emo2.MainActivity.imp(MainActivity.java:56) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at com.example.emo2.MainActivity.click(MainActivity.java:146) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at com.example.emo2.MainActivity.onClick(MainActivity.java:134) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at android.view.View.performClick(View.java:4212) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at android.view.View$PerformClick.run(View.java:17476) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at android.os.Handler.handleCallback(Handler.java:800) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at android.os.Handler.dispatchMessage(Handler.java:100) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at android.os.Looper.loop(Looper.java:194) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at android.app.ActivityThread.main(ActivityThread.java:5371) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at java.lang.reflect.Method.invoke(Method.java:525) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
04-03 05:11:14.084: E/AndroidRuntime(9317):  at dalvik.system.NativeStart.main(Native Method) 

` 等待解決方案....

+4

*「但它不工作」 *你能詳細談談那一點? – codeMagic 2015-04-02 22:20:42

+0

開啓3秒後,它正在中止應用程序...顯示不幸您的應用程序停止工作 – 2015-04-02 22:44:58

+0

然後從崩潰中發佈堆棧跟蹤 – codeMagic 2015-04-02 22:54:17

回答

2

您無法從非主線程更新UI /視圖元素。必須在UI線程上更新至textView。下面應該工作:

用以下替換您run()方法的內容:

runOnUiThread(new Runnable() { 
    public void run() { 
     t3.setText("times up"); 
     t3.setVisibility(View.VISIBLE); 
    } 
}); 
+0

,但在此之後,如果我點擊任何按鈕,它又崩潰了 – 2015-04-02 23:42:43

+1

這將是一個不同的問題。 – 2015-04-03 00:08:42

相關問題