正如我在问题标题中所说的那样,我正在尝试用我的mysql数据库中的项目填充我的Home Activity操作栏微调器......我正在调整我的私有GetFeeds类(AsyncTask扩展类)来获得我想要的东西来自活动的onCreate方法中的数据库..我正确地得到了结果,但随后应用停止并关闭,我甚至看不到手机中的活动......
我的HomeActivity:
public class HomeActivity extends ActionBarActivity implements
ActionBar.OnNavigationListener {
InternetConnectivityManager icm;
ProgressDialog pDialog;
BDFunctions bdfunctions;
String[] feeds;
/**
* The serialization (saved instance state) Bundle key representing the
* current dropdown position.
*/
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Set up the action bar to show a dropdown list.
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
icm = new InternetConnectivityManager(getApplicationContext());
new GetFeeds().execute();
// Set up the dropdown list navigation in the action bar.
actionBar.setListNavigationCallbacks(
// Specify a SpinnerAdapter to populate the dropdown list.
new ArrayAdapter<String>(actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1, feeds), this);
}
private class GetFeeds extends AsyncTask<Void, Void, JSONObject> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(HomeActivity.this);
pDialog.setMessage("A vrificar os grupos onde estas inserido...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected JSONObject doInBackground(Void... arg0) {
bdfunctions = new BDFunctions(getApplicationContext());
JSONObject json = bdfunctions.getFeeds();
return json;
}
protected void onPostExecute(JSONObject json) {
// dismiss the dialog after getting the result
pDialog.dismiss();
if(json != null) {
try {
if (json.getString(JSONKeys.KEY_SUCCESS) != null) {
String res = json.getString(JSONKeys.KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
JSONArray jsonFeeds = json.getJSONArray("feeds");
feeds = new String[jsonFeeds.length()];
for(int i=0; i<jsonFeeds.length(); i++) {
JSONObject feedOBJ = (JSONObject) jsonFeeds.get(i);
feeds[i] = feedOBJ.getString("descricao");
}
}else{
Toast.makeText(getApplicationContext(), json.getString(JSONKeys.KEY_ERROR_MSG), Toast.LENGTH_LONG).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "Ocorreu um erro com o servidor", Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore the previously serialized current dropdown position.
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getSupportActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// Serialize the current dropdown position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getSupportActionBar()
.getSelectedNavigationIndex());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, 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);
}
@Override
public boolean onNavigationItemSelected(int position, long id) {
// When the given dropdown item is selected, show its contents in the
// container view.
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container,
PlaceholderFragment.newInstance(position + 1)).commit();
return true;
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
TextView textView = (TextView) rootView
.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
}
Logcat说错误是由此行的java.lan.NullPointerException引起的:
new ArrayAdapter<String>(actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1, feeds), this);
我试图将setListNavigationCallbacks方法及其内部的所有内容从我的私有类移到onPostExecute方法中,所以我只在填充结果时填充微调器..但它给了我一个错误,我不能把setListNavigationCallbacks放在那里..我该怎么做?
由于
答案 0 :(得分:1)
GetFeeds
任务以异步方式执行。这意味着在完成之前无法创建ArrayAdapter
(或者feeds
数组将为null,因此异常)。
您应该将对setListNavigationCallbacks()
的调用移至任务的onPostExecute()
方法。