gpt4 book ai didi

java - ProgressDialog 不会消失!空指针异常

转载 作者:行者123 更新时间:2023-11-29 22:10:06 25 4
gpt4 key购买 nike

我已经为 android 2.3.3 制作了一个 android 套接字客户端应用程序。它发送一个 XML 请求,然后从一个 cobol 套接字服务器接收一个回复。

当客户端应用程序连接到服务器、发送和接收数据时,我希望弹出一个进度对话框。

它确实弹出了(虽然有点晚了),但我不能在没有得到的情况下关闭它空指针异常

我的代码:

.

public class OctopusActivity extends Activity implements OnClickListener 
{
//My variables
ProgressDialog progressDialog;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);

Varenummer = (EditText) findViewById(R.id.editText1);
Varetekst = (EditText) findViewById(R.id.editText2);
Gruppe = (EditText) findViewById(R.id.editText3);
Producent = (EditText) findViewById(R.id.editText4);
Enhed = (EditText) findViewById(R.id.editText5);
Pris = (EditText) findViewById(R.id.editText6);

}
class ClientThread implements Runnable
{


public void run()

{

try
{
Korer = true;
Log.d("Nicklas", "Thread Igang");
Socket socket = new Socket(serverIpAddress, serverPort);
socket.setSoTimeout(5000);
Log.d("Nicklas", "socket lavet");

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
Log.d("Nicklas", "Printer Oprettet");

String request = ("XML Request");
out.println(request);
out.flush();

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d("Nicklas", "Reader Oprettet");
Vector<String> v = new Vector<String>();
int i = 0;
Boolean KeepGoing = true;
while (KeepGoing)
{

try
{
String lol = in.readLine();
if (lol.contains("</Answer>")) { KeepGoing = false; }
v.add(new String(lol));
i++;
}
catch (Exception e)
{
Log.d("NickEEEXX", e.toString());
KeepGoing = false;
}
}

Log.d("Trolo", String.valueOf(i) );

in.close();
out.close();
socket.close();

String[] InputLinie = new String[i];

v.toArray(InputLinie);

// This is where i treat the data

Korer = false;
Log.d("NicklasMEH", "KAgemand!");
// The error comes on the following line:
progressDialog.dismiss();

}
catch (Exception e)
{
Log.d("NicklasEx", e.toString());
varetekst = "Kunne ikke forbinde til server";
Korer = false;

}

}

}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Varetekst.setText("");
Gruppe.setText("");
Producent.setText("");
Enhed.setText("");
Pris.setText("");
Log.d("Nicklas", "Starter thread");
progressDialog.show(this, "", "Loading");

if (!Korer)
{
Thread thread = new Thread(new ClientThread());
thread.start();

}

}

}

所以我的问题是:1. dialogprocess弹出的有点晚。我想让它弹出,然后我想让线程开始。2. 为什么在 progressDialog.dismiss(); 上出现空指针异常?

如有任何帮助,我们将不胜感激。

最佳答案

你把 progressDialog 放在哪里?

progressDialog =ProgressDialog.show(OctopusActivity.this,"title","message");

关于java - ProgressDialog 不会消失!空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9824379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com