2 sinat 27681957 sinat_27681957 于 2016.03.03 21:05 提问

一个fragment跳转到其它fragment,其它fragment变成了空白页 1C

MainActivity里面有三个fragment的样子,但是呢当我第一个fragment制作了获取新闻功能之后,其它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)

再次谢谢各位啦!!!

4个回答

devmiao
devmiao   Ds   Rxr 2016.03.03 21:47
sinat_27681957
sinat_27681957 谢谢啦 再帮我看一下吧
接近 2 年之前 回复
devmiao
devmiao   Ds   Rxr 2016.03.03 21:47
wen_wxpk
wen_wxpk   2016.03.04 08:24

把代码贴出来啊,哥们

wen_wxpk
wen_wxpk 回复sinat_27681957: 新闻是从服务器获得的吗,有Internet权限?有开多线程?
接近 2 年之前 回复
wen_wxpk
wen_wxpk 回复sinat_27681957: 新闻是从服务器获得的吗,有Internet权限?有开多线程?
接近 2 年之前 回复
sinat_27681957
sinat_27681957 谢谢啦,再帮我看一下吧
接近 2 年之前 回复
linghugoolge
linghugoolge   2017.05.29 20:14

楼主解决了吗?碰到了同样的问题了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!