我正在尝试使用AsyncTasks
功能创建一个简单的应用程序。问题出在onProgressUpdate()
。每当尝试运行我的应用程序时,我都会收到 java.lang.NullPointerException 。我查了LogCat
。但我无法找到错误的来源。
这是第94行。而eclipse说它是空的。
progress_bar.setProgress(values[0]);
这是输出
05-19 05:43:07.895: E/AndroidRuntime(2038): FATAL EXCEPTION: main
05-19 05:43:07.895: E/AndroidRuntime(2038): Process: com.example.ssdsd, PID: 2038
05-19 05:43:07.895: E/AndroidRuntime(2038): java.lang.NullPointerException
05-19 05:43:07.895: E/AndroidRuntime(2038): at com.example.ssdsd.MainActivity$MyAsyn.onProgressUpdate(MainActivity.java:94)
05-19 05:43:07.895: E/AndroidRuntime(2038): at com.example.ssdsd.MainActivity$MyAsyn.onProgressUpdate(MainActivity.java:1)
05-19 05:43:07.895: E/AndroidRuntime(2038): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:648)
05-19 05:43:07.895: E/AndroidRuntime(2038): at android.os.Handler.dispatchMessage(Handler.java:102)
05-19 05:43:07.895: E/AndroidRuntime(2038): at android.os.Looper.loop(Looper.java:137)
05-19 05:43:07.895: E/AndroidRuntime(2038): at android.app.ActivityThread.main(ActivityThread.java:4998)
05-19 05:43:07.895: E/AndroidRuntime(2038): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 05:43:07.895: E/AndroidRuntime(2038): at java.lang.reflect.Method.invoke(Method.java:515)
05-19 05:43:07.895: E/AndroidRuntime(2038): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-19 05:43:07.895: E/AndroidRuntime(2038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-19 05:43:07.895: E/AndroidRuntime(2038): at dalvik.system.NativeStart.main(Native Method)
这是我的代码
public class MainActivity extends Activity {
static final int SLEEP_TIME=(15*1000)/100;
Dialog dialog;
ProgressBar progress_bar;
TextView tvper;
Button btnstart;
MyAsyn asynClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnstart=(Button)findViewById(R.id.start);
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
asynClass=new MyAsyn();
asynClass.execute();
}
});
}
public class MyAsyn extends AsyncTask<Void, Integer, Void>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.progress_view);
progress_bar=(ProgressBar)findViewById(R.id.pgbar);
tvper=(TextView)findViewById(R.id.tvper);
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < 100; i++) {
if(isCancelled()){
break;
}
else {
try {
publishProgress(i);
Thread.sleep(SLEEP_TIME);
} catch (Exception e) {
// TODO: handle exception
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
try {
progress_bar.setProgress(values[0]);
} catch (Exception e) {
e.printStackTrace();
}
// tvper.setText("Loading "+values[0]+"%");
}
答案 0 :(得分:0)
您的progress_bar为空。在使用之前,您需要使用findViewById(R.id.xyz)找到它。