Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 进度条中的错误_Java_Android_Eclipse - Fatal编程技术网

Java 进度条中的错误

Java 进度条中的错误,java,android,eclipse,Java,Android,Eclipse,我在进度条中有一个错误 这是一个代码: public class BluetoothChat extends Activity { // Debugging private static final String TAG = "BluetoothChat"; private static final boolean D = true; private int progressStatus = 0; private Handler handler = new Handler(); // Me

我在进度条中有一个错误

这是一个代码:

public class BluetoothChat extends Activity {
// Debugging
private static final String TAG = "BluetoothChat";
private static final boolean D = true;

private int progressStatus = 0;

private Handler handler = new Handler();

// Message types sent from the BluetoothChatService Handler
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;

// Key names received from the BluetoothChatService Handler
public static final String DEVICE_NAME = "device_name";
public static final String TOAST = "toast";

// Intent request codes
private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
private static final int REQUEST_ENABLE_BT = 3;

// Layout Views
private ListView mConversationView;
private EditText mOutEditText;
private Button mSendButton;
private Button mvc;

// Name of the connected device
private String mConnectedDeviceName = null;
// Array adapter for the conversation thread
private ArrayAdapter<String> mConversationArrayAdapter;
// String buffer for outgoing messages
private StringBuffer mOutStringBuffer;
// Local Bluetooth adapter
private BluetoothAdapter mBluetoothAdapter = null;
// Member object for the chat services
private BluetoothChatService mChatService = null;
private ProgressBar progressBar;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mvc = (Button) findViewById(R.id.button1);

mvc.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new Thread(new Runnable() {
                public void run() {
                    while (progressStatus < 1000) {
                        Random r = new Random();
                        int i1 = r.nextInt(1000);
                        if (i1 > progressStatus){
                            progressStatus = i1;
                        }
                        else {
                            progressStatus = progressStatus;
                        }
                        // Update the progress bar and display the current value in the text view
                        handler.post(new Runnable() {
                            public void run() {
                                progressBar.setProgress(progressStatus);

                            }
                        });
                        try {
                            // Sleep for 200 milliseconds. Just to display the progress slowly
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    });
应用程序没有启动,有人能帮我理解这个问题吗?非常感谢。 P.S如果我删除按钮,仍然可以正常工作。

在onCreate()中,需要调用

setContentView(R.layout.activity_main);
或者您的布局名称-您的“button1”所在的xml

以前

mvc = (Button) findViewById(R.id.button1);  
它有助于您的活动识别其布局以及在何处查找具有
findViewById

将活动内容设置为显式视图。此视图直接放置在活动的视图层次结构中

添加

progressBar = new ProgressBar(this);
在您的
onCreate
方法中。您已经声明了一个变量,但尚未初始化它。你应该有使用权

setContentView(); 
设置活动的布局。这是医生

从doc中画出的线是:-

将活动内容设置为显式视图。此视图直接放置在活动的视图层次结构中。它本身可以是一个复杂的视图层次结构

setContentView();