我在进度条中出错。
这是一段代码:
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();
}
});
这是一个logcat:
02-15 12:14:14.102: E/MoreInfoHPW_ViewGroup(21179): Parent view is not a TextView
02-15 12:14:14.142: D/AndroidRuntime(21179): Shutting down VM
02-15 12:14:14.142: W/dalvikvm(21179): threadid=1: thread exiting with uncaught exception (group=0x418ca898)
02-15 12:14:14.142: E/AndroidRuntime(21179): FATAL EXCEPTION: main
02-15 12:14:14.142: E/AndroidRuntime(21179): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.BluetoothChat/com.example.android.BluetoothChat.BluetoothC hat}: java.lang.NullPointerException
02-15 12:14:14.142: E/AndroidRuntime(21179): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
02-15 12:14:14.142: E/AndroidRuntime(21179): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
02-15 12:14:14.142: E/AndroidRuntime(21179): at android.app.ActivityThread.access$700(ActivityThread.java:159)
02-15 12:14:14.142: E/AndroidRuntime(21179): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
02-15 12:14:14.142: E/AndroidRuntime(21179): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 12:14:14.142: E/AndroidRuntime(21179): at android.os.Looper.loop(Looper.java:137)
02-15 12:14:14.142: E/AndroidRuntime(21179): at android.app.ActivityThread.main(ActivityThread.java:5419)
02-15 12:14:14.142: E/AndroidRuntime(21179): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 12:14:14.142: E/AndroidRuntime(21179): at java.lang.reflect.Method.invoke(Method.java:525)
02-15 12:14:14.142: E/AndroidRuntime(21179): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-15 12:14:14.142: E/AndroidRuntime(21179): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-15 12:14:14.142: E/AndroidRuntime(21179): at dalvik.system.NativeStart.main(Native Method)
02-15 12:14:14.142: E/AndroidRuntime(21179): Caused by: java.lang.NullPointerException
02-15 12:14:14.142: E/AndroidRuntime(21179): at com.example.android.BluetoothChat.BluetoothChat.onCreate(BluetoothChat.java:98)
02-15 12:14:14.142: E/AndroidRuntime(21179): at android.app.Activity.performCreate(Activity.java:5372)
02-15 12:14:14.142: E/AndroidRuntime(21179): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-15 12:14:14.142: E/AndroidRuntime(21179): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
02-15 12:14:14.142: E/AndroidRuntime(21179): ... 11 more
02-15 12:14:16.534: I/Process(21179): Sending signal. PID: 21179 SIG: 9
应用程序无法启动,任何人都可以帮我理解这个问题吗?谢谢! P.S如果我取下按钮仍然可以正常工作。
答案 0 :(得分:0)
在onCreate()中,需要调用
setContentView(R.layout.activity_main);
或您的布局名称 - 您的“button1”为...的xml
之前
mvc = (Button) findViewById(R.id.button1);
它可以帮助您的活动识别其布局以及在哪里查找具有findViewById
将活动内容设置为显式视图。此视图直接放在活动的视图层次结构中。
http://developer.android.com/reference/android/app/Activity.html#setContentView(android.view.View)
http://bloggerinme.wordpress.com/2011/12/10/android-programming-setcontentview/
答案 1 :(得分:0)
添加
progressBar = new ProgressBar(this);
在您的onCreate
方法中。您已声明了一个变量,但未对其进行初始化。你应该使用
setContentView();
设置活动的布局。以下是setContentView
的文档从doc中提取的行是: -
将活动内容设置为显式视图。此视图直接放在活动的视图层次结构中。它本身可以是一个复杂的视图层次结构。