当我点击按钮显示ProgressDialog
时,我遇到了这个错误03-08 14:29:00.169 8564-8564/com.mohammedmoaayed.test2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mohammedmoaayed.test2, PID: 8564
Theme: themes:{}
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5471)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5471)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.setTitle(java.lang.CharSequence)' on a null object reference
at com.mohammedmoaayed.test2.MainActivity.buclick(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5471)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是我的java代码
package com.mohammedmoaayed.test2;
import android.app.Activity;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
Activity myac;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myac=this;
}
mythread t1;
ProgressDialog bardown;
public void buclick(View view) {
bardown.setTitle("Downloaing ");
bardown.setMessage("Download is >>>>");
bardown.setProgressStyle(bardown.STYLE_HORIZONTAL);
bardown.setProgress(0);
bardown.setMax(20);
bardown.show();
t1 = new mythread();
t1.start();
}
class mythread extends Thread {
public void run (){
while (bardown.getProgress()<20) {
myac.runOnUiThread(new Runnable() {
@Override
public void run() {
bardown.incrementProgressBy(1);
}
});
try {
Thread.sleep(1000);
} catch (Exception ex) {}
}
myac.runOnUiThread(new Runnable() {
@Override
public void run() {
bardown.dismiss();
}
});
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mohammedmoaayed.test2.MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="395dp"
android:layout_height="587dp"
android:onClick="buclick"
android:text="Button"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
答案 0 :(得分:0)
你在null对象上调用A
。你必须首先实例化它;
setTitle()
或者如果您动态创建它,请使用此
bardown = (ProgressBar)findViewById(R.id.bardown);
答案 1 :(得分:0)
您没有对ProgressDialog
进行初始化,因此请初始化ProgressDialog
“bardown”
在活动中初始化
bardown = new ProgressDialog(this);// write this line
在片段中初始化
bardown = new ProgressDialog(getActivity());// write this line
例如。
public void buclick(View view) {
bardown = new ProgressDialog(this);// write this line
bardown.setTitle("Downloaing ");
bardown.setMessage("Download is >>>>");
bardown.setProgressStyle(bardown.STYLE_HORIZONTAL);
bardown.setProgress(0);
bardown.setMax(20);
bardown.show();
t1 = new mythread();
t1.start();
}
答案 2 :(得分:0)
您尚未初始化ProgressDialog
。在使用之前初始化它。在调用setTitle
方法之前添加以下行。
bardown = new ProgressDialog(MainActivity.this);
答案 3 :(得分:0)
使用此
初始化对话框
ProgressDialog dialog = new ProgressDialog(Activity_Map_Advance_Tracking.this);dialog.setMessage("Loading....");
dialog.show();