问题描述
- 一个fragment跳转到其它fragment,其它fragment变成了空白页
-
MainActivity里面有三个fragment的样子,但是呢当我第一个fragment制作了获取新闻功能之后,其它fragment都变成了空白页,请高手指点一下下,谢啦!!
新闻fragment:
其它fragment:
MainActivity:import net.loonggg.fragment.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends FragmentActivity {
private Fragment[] mFragments;
private RadioGroup bottomRg;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private RadioButton rbOne, rbTwo, rbThree;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mFragments = new Fragment[3]; fragmentManager = getSupportFragmentManager(); mFragments[0] = fragmentManager.findFragmentById(R.id.fragement_main); mFragments[1] = fragmentManager.findFragmentById(R.id.fragement_bbs); mFragments[2] = fragmentManager.findFragmentById(R.id.fragement_setting); fragmentTransaction = fragmentManager.beginTransaction() .hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]); fragmentTransaction.show(mFragments[0]).commit(); setFragmentIndicator(); } private void setFragmentIndicator() { bottomRg = (RadioGroup) findViewById(R.id.bottomRg); rbOne = (RadioButton) findViewById(R.id.rbOne); rbTwo = (RadioButton) findViewById(R.id.rbTwo); rbThree = (RadioButton) findViewById(R.id.rbThree); bottomRg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { fragmentTransaction = fragmentManager.beginTransaction() .hide(mFragments[0]).hide(mFragments[1]) .hide(mFragments[2]); switch (checkedId) { case R.id.rbOne: fragmentTransaction.show(mFragments[0]).commit(); break; case R.id.rbTwo: fragmentTransaction.show(mFragments[1]).commit(); break; case R.id.rbThree: fragmentTransaction.show(mFragments[2]).commit(); break; default: break; } } }); }
}
显示新闻的fragment:
import java.util.ArrayList;
import java.util.List;import org.json.JSONArray;
import org.json.JSONException;import com.askdoctor.adapter.NewsAdapter;
import com.askdoctor.constans.Constant;
import com.askdoctor.entity.News;
import com.askdoctor.utils.HttpUtils;
import net.loonggg.fragment.R;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;public class FragmentMain extends Fragment {
private NewsAdapter adapter; private List<News> newsList; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_main, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ListView listNews = (ListView)getView().findViewById(R.id.list_web_view); newsList=new ArrayList<News>(); adapter = new NewsAdapter(getActivity(),newsList); listNews.setAdapter(adapter); HttpUtils.getNewsJSON(Constant.URL_GET_NEWS_LIST, getNewsHandler); } public Handler getNewsHandler=new Handler(){ public void handleMessage(android.os.Message msg){ String jsonData=(String)msg.obj; System.out.println(jsonData); try { JSONArray jsonArray=new JSONArray(jsonData); for (int i = 0; i < jsonArray.length(); i++) { org.json.JSONObject object = jsonArray.getJSONObject(i); String title=object.getString("title"); String desc=object.getString("desc"); String time=object.getString("time"); String content_url=object.getString("content_url"); String pic_url=object.getString("pic_url"); newsList.add(new News(title, desc, time, content_url, pic_url)); } //数据变化后更新adapter adapter.notifyDataSetChanged(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; };
}
变成空白页的fragment:
import net.loonggg.fragment.R;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;public class FragmentBBS extends Fragment {
Button btnSendBBS;@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_bbs, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); btnSendBBS=(Button)getView().findViewById(R.id.btn_jump_submit); initview(); } private void initview() { btnSendBBS.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(getActivity(),MakeBBS.class); startActivity(intent); } }); } @Override public void onPause() { super.onPause(); }
}
这是logcat:
03-03 20:26:52.056: E/memtrack(3342): Couldn't load memtrack module (No such file or directory)
03-03 20:26:52.056: E/android.os.Debug(3342): failed to load memtrack module: -2
03-03 20:26:52.756: E/InputDispatcher(372): channel '4a9f03c8 net.loonggg.fragment/com.askdoctor.ui.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
03-03 20:26:54.940: E/Icing(685): Error initializing, resetting corpora: Could not create native index
03-03 20:26:55.240: E/Icing(685): Internal init failed
03-03 20:26:55.240: E/Icing(685): Couldn't handle android.intent.action.PACKAGE_REMOVED intent due to initialization failure.
03-03 20:26:55.240: E/Icing(685): Couldn't handle android.intent.action.PACKAGE_ADDED intent due to initialization failure.
03-03 20:26:55.244: E/Icing(685): Couldn't handle android.intent.action.PACKAGE_REPLACED intent due to initialization failure.
03-03 20:26:57.100: E/memtrack(3367): Couldn't load memtrack module (No such file or directory)
03-03 20:26:57.100: E/android.os.Debug(3367): failed to load memtrack module: -2
03-03 20:26:57.944: E/memtrack(3388): Couldn't load memtrack module (No such file or directory)
03-03 20:26:57.944: E/android.os.Debug(3388): failed to load memtrack module: -2
03-03 20:32:09.552: E/CheckinTask(685): Checkin failed: https://android.clients.google.com/checkin (request #0): org.apache.http.conn.ConnectTimeoutException: Connect to /74.125.23.138:443 timed out
03-03 20:35:50.196: E/BandwidthController(138): Updating quota globalAlert failed (No such file or directory)再次谢谢各位啦!!!
解决方案
http://bbs.csdn.net/topics/390917966
解决方案二:
把代码贴出来啊,哥们
解决方案三:
参考下
http://dwtedx.com/itshare_296.html