添加的textview代码在模拟器中崩溃了应用程序

时间:2011-12-26 22:17:39

标签: android android-emulator textview progress-bar

我正在做一个家庭作业项目,需要有一个textview计数器和一个现有的进度条。我将textview代码添加到java文件和main.xml中。当我在模拟器中运行它时,我得到“应用程序xxx意外停止”消息。我无法弄清楚原因。这是我的代码: (代码缩进不完全正确。我会努力做到这一点) 源文件:

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;
    }

}

我只在现有代码中添加了几行(包括对main.xml的textview)。那么它比简单地添加textview代码来实现textview计数器更复杂吗?鉴于它是我的介绍级课程中的第一个项目之一,我认为简单地添加textview应该满足要求。请指教。谢谢!

1 个答案:

答案 0 :(得分:0)

我测试了你的代码,它对我有用!你试过:

  1. 在Eclipse栏项目 - >清洁项目
  2. 右键点击您的项目Android工具 - >修复项目属性