我已经检查了文档是否有更改和可能的修复,但现在我使用您提供的链接提供的新代码获取活动ComponentInfo错误。编辑下面的代码。
MainActivity.java
public class TabMainActivity extends Activity {
ActionBar.Tab Tab1, Tab2, Tab3;
FragmentTab1 fragmentTab1 = new FragmentTab1();
FragmentTab2 fragmentTab2 = new FragmentTab2();
FragmentTab3 fragmentTab3 = new FragmentTab3();
ArrayList<ListData> dataArray;
ListView listView;
ArrayList<DATA> data = new ArrayList<DATA>();
private static String url = "http://80.240.142.93/nokta/casestudy.json";
private static final String TAG_VIDEO = "videos";
private static final String TAG_TITLE = "title";
private static final String TAG_THUMB = "thumbnail";
private static final String TAG_LINK = "video_url";
private ArrayList<HashMap<String, String>> videosList;
Bundle bund = new Bundle();
private Intent intent;
private JSONArray listArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
bund.putInt("love", 5);
fragmentTab1.setArguments(bund);
setContentView(R.layout.tabmainactivity);
videosList = new ArrayList<HashMap<String,String>>();
if (isNetworkAvailable()){
new JSONParse().execute();
}else{
showMessage("There is no internet connection");
}
android.app.FragmentManager fm = getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
//ft.replace(R.id.fragment_container, fragmentTab1);
//ft.addToBackStack(null);
//ft.commit();
ActionBar actionBar = getActionBar();
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(false);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(false);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab1 = actionBar.newTab().setText("TAB1").setTabListener(new TabListener<FragmentTab1>(this,"tab1",FragmentTab1.class));
actionBar.addTab(Tab1);
Tab2 = actionBar.newTab().setText("TAB2").setTabListener(new TabListener<FragmentTab2>(this,"tab2",FragmentTab2.class));
actionBar.addTab(Tab2);
Tab3 = actionBar.newTab().setText("TAB3").setTabListener(new TabListener<FragmentTab3>(this,"tab3",FragmentTab3.class));
actionBar.addTab(Tab3);
//ListView listV = fragmentTab1.getListView();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONfromURL(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
listArray = json.getJSONArray(TAG_VIDEO);
{
prepareData(json);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
private void showMessage(String txt){
Toast.makeText(this, txt, Toast.LENGTH_LONG).show();
}
private void prepareData(JSONObject json){
try {
listArray = json.getJSONArray(TAG_VIDEO);
dataArray = new ArrayList<ListData>();
for (int i = 0; i < listArray.length(); i++){
// tmp hashmap for single contact
HashMap<String, String> video = new HashMap<String, String>();
JSONObject c = listArray.getJSONObject(i);
String jpeg_s = c.getString(TAG_THUMB);
String title_s = c.getString(TAG_TITLE);
String video_s = c.getString(TAG_LINK);
video.put(TAG_THUMB, jpeg_s);
video.put(TAG_TITLE, title_s);
video.put(TAG_LINK, video_s);
videosList.add(video);
try {
URL newurl = new URL(jpeg_s);
Bitmap mIcon_val;
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
ListData prepare_data = new ListData();
prepare_data.setImage(mIcon_val);
prepare_data.setName(title_s);
dataArray.add(prepare_data);
Log.e("BitMap", mIcon_val.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//android.app.FragmentManager fm = getFragmentManager();
//android.app.FragmentTransaction ft = fm.beginTransaction();
//ft.replace(R.id.fragment_container, fragmentTab1);
//ft.addToBackStack(null);
//ft.commit();
ListView lw = fragmentTab1.getListView();
lw.setAdapter(new CustomAdapter(this, dataArray));
} catch (JSONException e) {
e.printStackTrace();
}
/*
listView.setAdapter(new CustomAdapter(this, dataArray));
Log.e("LW", listView.getItemAtPosition(1).toString());*/
Log.e("returnJSON", videosList.toString());
}
}
TabListener.java
public class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
String backStateName = mFragment.getTag();
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
Log.e("BAKSTAE", backStateName);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
有人看到了这个问题吗? 感谢。
答案 0 :(得分:0)
问题是当你切换标签时你正在更换片段,这告诉Android你不再需要以前的片段了。因此,当您切换回它时,它所拥有的数据就消失了。
这里的文档有一个很好的示例,说明如何实现TabListener: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
在他们的例子中,他们延迟实例化Fragment实例(而不是像你一样在字段初始化器中创建所有片段)。然后,如果片段是新的,则调用ft.add
,如果不是,则调用ft.attach
。此外,当取消选中该标签时,他们会调用ft.detach
。
答案 1 :(得分:0)
尝试以下代码
public class EventGPS {
private boolean GPSMessage;
public boolean getGPSMessage() {
return GPSMessage;
}
public void setGPSMessage(boolean GPSMessage) {
this.GPSMessage = GPSMessage;
}
}