当前位置: 代码迷 >> Android >> 新手。fragment空指针
  详细解决方案

新手。fragment空指针

热度:75   发布时间:2016-04-28 02:09:11.0
新手求助。。。fragment空指针
package com.example.xywy.fragment;

import com.example.xywy.R;
import com.example.xywy.activity.QueMainActivity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class QueMainFragment extends Fragment implements OnClickListener{
private TextView ok, age, into;
private RadioGroup radioGroup;
private RadioButton man, woman;
private ImageView image1, image2, image3, image4,back;
public FragmentTransaction ft;
private FragmentManager fm;
private Fragment fragment1,fragment2;
private QueMainActivity main = new QueMainActivity();
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(
com.example.xywy.R.layout.jbzs_question_layout, null);
return v;
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
initView();
}




private void initView() {
fragment1 = new QueFragment();
fragment2 = new QueFFragment();
fragment1 = getActivity().getSupportFragmentManager().findFragmentById(R.id.que_fragment);
fragment2 = getActivity().getSupportFragmentManager().findFragmentById(R.id.quef_fragment);
back = (ImageView) getActivity().findViewById(R.id.jbzs_question_back);
ok = (TextView)getActivity(). findViewById(R.id.jbzs_question_ok);
age = (TextView)getActivity().findViewById(R.id.jbzs_question_age);
into = (TextView)getActivity(). findViewById(R.id.jbzs_question_ed);
radioGroup = (RadioGroup) getActivity().findViewById(R.id.jbzs_question_radiogroup);
man = (RadioButton) getActivity().findViewById(R.id.man);
woman = (RadioButton) getActivity().findViewById(R.id.woman);
image1 = (ImageView) getActivity().findViewById(R.id.jbzs_add_photo1);
image2 = (ImageView) getActivity().findViewById(R.id.jbzs_add_photo2);
image3 = (ImageView)getActivity(). findViewById(R.id.jbzs_add_photo3);
image4 = (ImageView) getActivity().findViewById(R.id.jbzs_add_photo4);
back.setOnClickListener(this);
ok.setOnClickListener(this);
age.setOnClickListener(this);
into.setOnClickListener(this);
radioGroup.setOnClickListener(this);
man.setOnClickListener(this);
woman.setOnClickListener(this);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);


}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.jbzs_question_back:
getActivity().finish();
break;
case R.id.jbzs_question_ok:
break;
case R.id.jbzs_add_photo1:
ft.hide(fragment1);//这行报错了。。。。为什么
ft.commit();
break;
case R.id.jbzs_add_photo2:
break;
case R.id.jbzs_add_photo3:

break;
default:
break;
}

}

}

------解决思路----------------------
引用:
Quote: 引用:

ft还没有初始化。

这个初始化怎么闹。。刚开始学,,不太懂

你的ft是FragmentTransaction,所以,ft=fm.beginTransaction();在使用ft前得加上这句初始化。
------解决思路----------------------
ft,fm都没实例。
  相关解决方案