gpt4 book ai didi

java - 在辅助线程中填充的字符串数组,但主线程将数组显示为 null

转载 作者:行者123 更新时间:2023-11-30 09:10:52 25 4
gpt4 key购买 nike

我有一个辅助线程,它从网络接收(字符串数组)数据,然后将这些值存储在 public String array(已在辅助线程外声明)中,如下所示:

//Code to fetch result in XML format
URL GetCardXmlUrl = new URL("http://10.0.2.2:8080/searchresultxml/search_xml_output.xml");
URLConnection GetresultXmlConnection = GetCardXmlUrl.openConnection();
InputStream ResultInstream = new BufferedInputStream(GetresultXmlConnection.getInputStream());
Serializer PhoneBookSerializer = new Persister();

Search_info_xml parse_result_xml = PhoneBookSerializer.read(Search_info_xml.class, ResultInstream);
SearchResult.this.mobile_nos = parse_result_xml.getPhones();

Log.d("note:", "getting stuff");
Log.d("number: ", mobile_nos[0]);

这里的mobile_no是公共(public)变量。 Log.d("number: ", mobile_nos[0]);成功显示值。

但是当我尝试从主线程访问同一个数组(在辅助线程完成它之后)并尝试从中填充一个列表时,我得到了 java.lang.NullPointerException 错误.

这是我访问数组的方式:

try {
setListAdapter(new ArrayAdapter<String>(this, R.layout.phonebook_listview, mobile_nos));

} catch(Exception e) {

e.printStackTrace();
}

这是我声明 mobile_nos 数组的方式:public String[] mobile_nos = new String[20];

知道可能是什么问题吗?还是我做错了?

编辑:完整代码,按要求。

public class SearchResult extends ListActivity {

public volatile String[] mobile_nos = new String[20];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Get the search string sent by search.class via bundle
Bundle valBundle = getIntent().getExtras();
String searchString = valBundle.getString("search_string");
Log.d("company: ", searchString);

getSearchResult(searchString);

try {

setListAdapter(new ArrayAdapter<String>(this, R.layout.phonebook_listview, mobile_nos));
} catch(Exception e) {

e.printStackTrace();
}

ListView listView = getListView();
listView.setTextFilterEnabled(true);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the mobile number
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});

}

getSearchResult 方法定义:

public void getSearchResult(final String searchString){

/*--The connection to the server is established in another (not main) thread.
This prevents the app to stop responding if the connection to the server is not established
or it takes time to establish the connection--*/

new Thread(new Runnable() {
@Override
public void run() {

try {

//Send search string to the server
URL mobileurl = new URL("http://10.0.2.2:8080/project_server/Search");
URLConnection connection = mobileurl.openConnection();
connection.setDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(searchString);
out.close();

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
in.readLine();
in.close();

//Fetch result in XML format
URL GetCardXmlUrl = new URL("http://10.0.2.2:8080/searchresultxml/search_xml_output.xml");
URLConnection GetresultXmlConnection = GetCardXmlUrl.openConnection();
InputStream ResultInstream = new BufferedInputStream(GetresultXmlConnection.getInputStream());
Serializer PhoneBookSerializer = new Persister();

Search_info_xml parse_result_xml = PhoneBookSerializer.read(Search_info_xml.class, ResultInstream);
SearchResult.this.mobile_nos = parse_result_xml.getPhones();

Log.d("note:", "getting stuff");
Log.d("number: ", mobile_nos[0]);

} catch (Exception e) {

e.printStackTrace();
}

}
}).start();

}

}

最佳答案

你这样做:

getSearchResult(searchString);
try {
setListAdapter(new ArrayAdapter<String>(this, R.layout.phonebook_listview, mobile_nos));
} catch(Exception e) {
e.printStackTrace();
}

在 getSearchResult 中启动新线程,并在它结束之前运行 setListAdapter,移动编号为新的 String[20]。您忘记了 Thread 与您的其余代码同时运行。

尝试这样的事情:

public void getSearchResult(final String searchString){

/*--The connection to the server is established in another (not main) thread.
This prevents the app to stop responding if the connection to the server is not established
or it takes time to establish the connection--*/

new Thread(new Runnable() {
@Override
public void run() {

try {
//Send search string to the server
URL mobileurl = new URL("http://10.0.2.2:8080/project_server/Search");
URLConnection connection = mobileurl.openConnection();
connection.setDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(searchString);
out.close();

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
in.readLine();
in.close();

//Fetch result in XML format
URL GetCardXmlUrl = new URL("http://10.0.2.2:8080/searchresultxml/search_xml_output.xml");
URLConnection GetresultXmlConnection = GetCardXmlUrl.openConnection();
InputStream ResultInstream = new BufferedInputStream(GetresultXmlConnection.getInputStream());
Serializer PhoneBookSerializer = new Persister();

Search_info_xml parse_result_xml = PhoneBookSerializer.read(Search_info_xml.class, ResultInstream);

final String[] data = parse_result_xml.getPhones();

runOnUiThread(new Runnable(){
@Override
public void run() {
setListAdapter(new ArrayAdapter<String>(SearchResult.this, R.layout.phonebook_listview, data));
}
});

Log.d("note:", "getting stuff");
Log.d("number: ", mobile_nos[0]);

} catch (Exception e) {
e.printStackTrace();
}

}
}).start();

}

}

并移除

try {
setListAdapter(new ArrayAdapter<String>(this, R.layout.phonebook_listview, mobile_nos));
} catch(Exception e) {
e.printStackTrace();
}

调用 getSearchResult 方法后。您还可以删除 mobile_nos 字段

关于java - 在辅助线程中填充的字符串数组,但主线程将数组显示为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22333538/

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