以下是我的代码 - 我有一个简单的微调器来显示数组数据。我通过调用WebService获取数组。数组填充正确,但调用微调器时应用程序崩溃。
我试图在我的主要活动中调用AsyncTask并在onCreate活动之外声明我的数组,但是数组会自动初始化。
请帮忙 -
import android.support.v7.app.ActionBarActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends ActionBarActivity {
private String[] loan_type = new String[5];
String[] curr_arr = new String[5];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Call Web-Service to fill the drop down list
loan_type = getResources().getStringArray(R.array.loan_type);
AsyncCallWS task = new AsyncCallWS();
task.execute();
//*******************************************
}
//Create Async Task
private class AsyncCallWS extends AsyncTask<String[], Void, Void> {
@Override
protected Void doInBackground(String[]... params) {
//Invoke webservice
String[] temp_arr = new String[5];
Spinner spinner = (Spinner)findViewById(R.id.spinner1);
temp_arr = CallWebService.invokeHelloWorldWS("Hello","getCurrency");
if (temp_arr[0] != null){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, temp_arr);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
//Set response
//tv.setText(displayText);
//Make ProgressBar invisible
//pg.setVisibility(View.INVISIBLE);
}
@Override
protected void onPreExecute() {
//Make ProgressBar invisible
//pg.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
//**************************************************************************
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
下面&#39;工作代码 -
import android.support.v7.app.ActionBarActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends ActionBarActivity {
private String[] loan_type = new String[5];
String[] curr_arr = new String[5];
String[] temp_arr = new String[5];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Call Web-Service to fill the drop down list
loan_type = getResources().getStringArray(R.array.loan_type);
AsyncCallWS task = new AsyncCallWS();
task.execute();
//*******************************************
}
//Create Async Task
private class AsyncCallWS extends AsyncTask<String[], Void, Void> {
@Override
protected Void doInBackground(String[]... params) {
//Invoke webservice
temp_arr = CallWebService.invokeHelloWorldWS("Hello","getCurrency");
return null;
}
@Override
protected void onPostExecute(Void result) {
//Set response
//tv.setText(displayText);
//Make ProgressBar invisible
//pg.setVisibility(View.INVISIBLE);
if (temp_arr[0] != null){
Spinner spinner = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, temp_arr);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
}
@Override
protected void onPreExecute() {
//Make ProgressBar invisible
//pg.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
//**************************************************************************
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
doInBackground
是一种异步方法,您可以从中更改UI。你可以通过设置Spinner
来规则化。
将您在doInBackground中检索到的任何数据传递给在UI线程上运行的onPostExecute
,并在那里创建您的微调器。
如果您的应用程序崩溃了,请查看堆栈跟踪,其中大部分时间都会告诉您哪些错误。如果你问一个问题,也可以发布堆栈跟踪:)
答案 1 :(得分:1)
您必须在任务中的onPostExecute
上执行查看操作。
private class AsyncCallWS extends AsyncTask<String[], Void, String[]> {
@Override
protected String[] doInBackground(String[]... params) {
//Invoke webservice
String[] temp_arr = new String[5];
Spinner spinner = (Spinner)findViewById(R.id.spinner1);
temp_arr = CallWebService.invokeHelloWorldWS("Hello","getCurrency");
return temp_arr ;
}
@Override
protected void onPostExecute(String[] temp_arr) {
if (temp_arr[0] != null){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, temp_arr);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
}
@Override
protected void onPreExecute() {
//Make ProgressBar invisible
//pg.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}