其他分享
首页 > 其他分享> > android – 进度对话框不在屏幕上显示

android – 进度对话框不在屏幕上显示

作者:互联网

我根据亲爱的Mayank’answer编辑了我的代码,但它没有显示在方法开始之前在displayMsg()方法中作为输入发送的任何消息.我应该说使用nfc和方法onNewIntent(Intent intent)启动MethodTest()

@Override
protected void onNewIntent(Intent intent) {
   MethodTest();
    ..............

}

public void MethodTest() {
    DisplayMsg("method 1 is running");
    Method1();

    DisplayMsg("method 2 is running");
    Method2();

    DisplayMsg("method 3 is running");
    Method3();

}

private int DisplayMsg(String msg) {
    totalMsg += msg;
    DisplayMsgClass dc = new DisplayMsgClass();
    dc.doInBackground(totalMsg);
}

private class DisplayMsgClass extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
         textView.setText("Hello !!!");
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.VISIBLE);
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected String doInBackground(String... Messages) {


        return Messages[0];
    }

    @Override
    protected void onPostExecute(String result) {
        progressBar.setVisibility(View.INVISIBLE);

        textView.setText(result);
    }
}

在我的布局中:

<LinearLayout>
<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:id="@+id/progressBar1"
    />
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/textv1"
  android:hint="AppletPass"
  android:gravity="center"/>
 </LinearLayout>

解决方法:

请记住,理想情况下,AsyncTasks应该用于短操作(最多几秒钟).

尝试了解有关AsyncTask的更多信息,并且代码中存在很多错误

>不要手动调用doInBackground().

dc.doInBackground(totalMsg); //错误
> DisplayMsg()多次调用,每次创建一个新的DisplayMsgClass类实例

DisplayMsgClass dc = new DisplayMsgClass(); //错误
> onPreExecute()
textView.setText(“Hello !!!”); // 空指针异常.调用textView.setText()而不初始化它.

警告

不要在同一个intance上多次调用AsyncTask.execute().
例如:

DisplayMsgClass displayMsgClass = new DisplayMsgClass();  
displayMsgClass.execute();  
displayMsgClass.execute(); //Error, IllegalStateException  

将根据您的实现向您展示一个基本演示,您可以根据自己的方式进行修改.

public void MethodTest() {

    // execute task
    new DisplayMsgClass().execute("Download now");
}

/*
public void MethodTest() {
    DisplayMsg("method 1 is running");
    Method1();

    DisplayMsg("method 2 is running");
    Method2();

    DisplayMsg("method 3 is running");
    Method3();

}

private int DisplayMsg(String msg) {
    totalMsg += msg;
    DisplayMsgClass dc = new DisplayMsgClass();
    dc.doInBackground(totalMsg);
}
*/

private class DisplayMsgClass extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        // retrieve the widgets
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        textView = (TextView) findViewById(R.id.textv1);


        textView.setText("Download initialized");
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(String... Messages) {

        // read commands
        String command = Messages[0];
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Download completed";
    }

    @Override
    protected void onPostExecute(String result) {

        //invoked on the UI thread after the background computation finishes
        progressBar.setVisibility(View.INVISIBLE);
        textView.setText(result);
    }
} 

标签:android,nfc-p2p
来源: https://codeday.me/bug/20190527/1165350.html