2011-12-26 47 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

相关问题