Java Android:从Mysql填充操作栏微调器
正如我在问题标题中所说,我正在尝试用mysql数据库中的项目填充我的家庭活动操作栏微调器。。。我正在调用我的私有GetFeeds类(AsyncTask扩展类),以便从活动的onCreate方法中的数据库中获取所需的内容。。我得到的结果是正确的,但随后应用程序停止并关闭,我甚至看不到手机中的活动 我的家庭活动:Java Android:从Mysql填充操作栏微调器,java,android,Java,Android,正如我在问题标题中所说,我正在尝试用mysql数据库中的项目填充我的家庭活动操作栏微调器。。。我正在调用我的私有GetFeeds类(AsyncTask扩展类),以便从活动的onCreate方法中的数据库中获取所需的内容。。我得到的结果是正确的,但随后应用程序停止并关闭,我甚至看不到手机中的活动 我的家庭活动: public class HomeActivity extends ActionBarActivity implements ActionBar.OnNavigationListe
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;
}
}
公共类HomeActivity扩展了ActionBarActivity实现
ActionBar.OnNavigationListener{
Internet连接管理器icm;
ProgressDialog;
bd功能bd功能;
字符串[]提要;
/**
*表示
*当前下拉位置。
*/
私有静态最终字符串状态\u SELECTED\u NAVIGATION\u ITEM=“SELECTED\u NAVIGATION\u ITEM”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//设置操作栏以显示下拉列表。
最终ActionBar ActionBar=getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(actionBar.NAVIGATION\u MODE\u列表);
icm=新的InternetConnectionManager(getApplicationContext());
新建GetFeeds().execute();
//在操作栏中设置下拉列表导航。
actionBar.setListNavigationCallbacks(
//指定SpinnerAdapter以填充下拉列表。
新的ArrayAdapter(actionBar.getThemedContext(),
android.R.layout.simple\u list\u item\u 1,
android.R.id.text1,feeds),这个);
}
私有类GetFeeds扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
pDialog=新建进度对话框(HomeActivity.this);
pDialog.setMessage(“一个Vricar os grupos onde estas inserido…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的JSONObject doInBackground(无效…arg0){
bdfunctions=新的bdfunctions(getApplicationContext());
JSONObject json=bdfunctions.getFeeds();
返回json;
}
受保护的void onPostExecute(JSONObject json){
//得到结果后关闭对话框
pDialog.disclose();
if(json!=null){
试一试{
if(json.getString(JSONKeys.KEY_SUCCESS)!=null){
String res=json.getString(JSONKeys.KEY\u SUCCESS);
if(Integer.parseInt(res)==1){
JSONArray jsonFeeds=json.getJSONArray(“feeds”);
feeds=新字符串[jsonFeeds.length()];
对于(int i=0;iGetFeeds
任务异步执行。这意味着您无法创建ArrayAdapter
,直到它完成(或者feeds
数组将为空,因此出现异常)
您应该将对
setListNavigationCallbacks()
的调用移动到任务的onPostExecute()
方法中。请从logcat发布异常堆栈跟踪。刚刚更新了post@matiash
new ArrayAdapter<String>(actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1, feeds), this);