2011-12-26 36 views
0

我正在做一個家庭作業項目,該項目需要有一個textview計數器以及一個現有的進度條。我將textview代碼添加到java文件和main.xml中。當我在模擬器中運行它時,出現「應用程序xxx意外停止」消息。我一直無法找出原因。這裏是我的代碼: (代碼縮進不是完全正確的,我會努力讓它如此。) 源文件:添加的textview代碼在模擬器中崩潰了應用程序

package com.mypackage; 

import android.app.Activity; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MyButtonActivity extends Activity { 
    static final int PROGRESS_DIALOG = 0; 
    Button button; 
    ProgressThread progressThread; 
    ProgressDialog progressDialog; 
    TextView Int; 

/** Called when the activity is first created. */ 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Int = (TextView)findViewById(R.id.Int); 

    // Setup the button that starts the progress dialog 
    button = (Button) findViewById(R.id.myButton); 
    button.setOnClickListener(new Button.OnClickListener(){ 
     public void onClick(View v) { 
      // Show dialog managed by this activity 
      showDialog(PROGRESS_DIALOG); 
     } 
    }); 
} 

// Callback for creating dialogs that are managed (saved and restored) 
// for you by the activity. 
protected Dialog onCreateDialog(int id) { 
    switch(id) { 
    case PROGRESS_DIALOG: 
     progressDialog = new ProgressDialog(MyButtonActivity.this); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     progressDialog.setMessage("Loading..."); 

     // Create and start the handler 
     progressThread = new ProgressThread(handler); 
     progressThread.start(); 

     return progressDialog; 
    default: 
     return null; 
    } 
} 

// Define the Handler that receives messages from the thread and update 
// the progress 
final Handler handler = new Handler() { 
    public void handleMessage(Message msg) { 
     int total = msg.getData().getInt("total"); 
     progressDialog.setProgress(total); 
     Int.setText(String.valueOf(total)); 

     if (total >= 100){ 
      dismissDialog(PROGRESS_DIALOG); 
      progressThread.setState(ProgressThread.STATE_DONE); 
     } 
    } 
}; 

/** Nested class that performs progress calculations (counting) */ 
private class ProgressThread extends Thread { 
    Handler mHandler; 
    final static int STATE_DONE = 0; 
    final static int STATE_RUNNING = 1; 
    int mState; 
    int total; 

    ProgressThread(Handler h) { 
     mHandler = h; 
    } 

    public void run() { 
     mState = STATE_RUNNING; 
     total = 0; 
     while (mState == STATE_RUNNING) { 
      try { 
       Thread.sleep(100); 
      } catch (InterruptedException e) { 
       Log.e("ERROR", "Thread Interrupted"); 
      } 
      Message msg = mHandler.obtainMessage(); 
      Bundle b = new Bundle(); 
      b.putInt("total", total); 
      msg.setData(b); 
      mHandler.sendMessage(msg); 
      total++; 
     } 
    } 

    /* sets the current state for the thread, 
    * used to stop the thread */ 
    public void setState(int state) { 
     mState = state; 
    } 

} 

我只加了幾行,以現有的代碼(包括一個TextView來的main.xml )。那麼比單純添加textview代碼來實現textview計數器更爲重要?鑑於它是我的入門級課程的第一批項目之一,我認爲只需添加一個文本視圖即可滿足要求。請指教。謝謝!

+0

能否請您檢查logcat中和後的錯誤,它扔在這裏? – KSubedi 2011-12-26 22:20:47

回答

0

我測試了你的代碼,它對我有效!你有沒有嘗試:

  1. 在Eclipse的酒吧項目 - >清理項目
  2. 右鍵點擊你的項目的Android工具 - >修復項目屬性
+1

感謝您的提示!問題解決了:Logcat聲明我沒有爲textview指定layout_width。 – macrogeo 2011-12-28 04:59:59

相關問題