我是Android的新用户,我有一个Fragment
我有一个Button
,点击按钮我想显示另一个Fragment
代替Fragment
在同一个Activity中。我更改Fragment
的代码是这样的:
btnMainSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment generateRequistionFrag=new generate_material_requisition();
if (generateRequistionFrag != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, generateRequistionFrag).commit();
}
}
});
我收到这样的错误:
Process: kanix.highrise.com.highrise, PID: 3427
java.lang.ClassCastException: kanix.highrise.com.highrise.MainActivity@3992b1e7 must implement OnFragmentInteractionListener
at kanix.highrise.com.highrise.generate_material_requisition.onAttach(generate_material_requisition.java:83)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:853)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
我尝试实施OnFragmentInteractionListener
但没有工作。
我想要切换到其他片段的完整片段代码是这样的:
* Created by Mubashir.gul on 13/05/2015.
*/
public class MaterialRequisition extends Fragment {
Spinner drpProj,drpMaterial,drpTask;
ProgressDialog pbar;
private ImageButton btncal;
private Calendar cal;
private int day;
private int month;
private int year;
private EditText etcal;
EditText etQuantity,etSrProject,etSrMaterial,etSrTask;
List<CItem> lstItm;
CItem Itm;
Activity myactivity;
Button btnSearchProject,btnSearchMaterial,btnSearchTask,btnMainSearch;
String ProID,MatrID,TaskID;
public MaterialRequisition(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_material_requisition, container, false);
//code for calender
btncal = (ImageButton)rootView. findViewById(R.id.btnimgcal);
etcal = (EditText) rootView.findViewById(R.id.etDate);
etcal.setKeyListener(null);//setting edittext non editable
etQuantity = (EditText) rootView.findViewById(R.id.etQuantity);
drpProj = (Spinner) rootView.findViewById(R.id.drpProject);
drpMaterial = (Spinner) rootView.findViewById(R.id.drpMaterial);
drpTask = (Spinner) rootView.findViewById(R.id.drpTask);
btnSearchProject =(Button)rootView.findViewById(R.id.btnSearchProject);
btnSearchMaterial =(Button)rootView.findViewById(R.id.btnSearchMaterial );
btnSearchTask =(Button)rootView.findViewById(R.id.btnSearchTask );
btnMainSearch=(Button)rootView.findViewById(R.id.btnMainSearch);
etSrProject=(EditText)rootView.findViewById(R.id.etSearchProject);
etSrMaterial=(EditText)rootView.findViewById(R.id.etSearchMaterial);
etSrTask=(EditText)rootView.findViewById(R.id.etSearchTask);
myactivity=this.getActivity();//getting parent activity in a variable for use in async task
etQuantity.setKeyListener(null);//setting edittext non editable
//loading projects
btnSearchProject.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProjectAsync PAsync=new ProjectAsync();
PAsync.execute(etSrProject.getText().toString());
}
});
btnSearchMaterial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!drpProj.getSelectedItem().toString().equals("Select Project") ) {
MaterialAsync MAsync = new MaterialAsync();
MAsync.execute("64");
}
else
{
Toast.makeText(myactivity,"Please select project first!",Toast.LENGTH_SHORT);
}
}
});
btnSearchTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!drpProj.getSelectedItem().toString().equals("Select Project") ) {
ListTaskAsync taskasync = new ListTaskAsync();
taskasync.execute(ProID);
}
else
{
Toast.makeText(myactivity,"Please select project first!",Toast.LENGTH_SHORT);
}
}
});
btnMainSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment generateRequistionFrag=new generate_material_requisition();
if (generateRequistionFrag != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, generateRequistionFrag).commit();
}
}
});
btncal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getFragmentManager(), "DatePicker");
}
});
//code for calender
//call to async task
lstItm=new ArrayList<CItem>();
Itm=new CItem( "-1", "Select Project" );
lstItm.add(Itm);
if(lstItm.size()>0) {
ArrayAdapter<CItem> adapterProj = new ArrayAdapter<CItem>(myactivity, android.R.layout.simple_spinner_item, lstItm);
drpProj.setAdapter(adapterProj);
}
lstItm=new ArrayList<CItem>();
Itm=new CItem( "-1", "Select Material" );
lstItm.add(Itm);
if(lstItm.size()>0) {
ArrayAdapter<CItem> adapterMater = new ArrayAdapter<CItem>(myactivity, android.R.layout.simple_spinner_item, lstItm);
drpMaterial.setAdapter(adapterMater);
}
lstItm=new ArrayList<CItem>();
Itm=new CItem( "-1", "Select Task" );
lstItm.add(Itm);
if(lstItm.size()>0) {
ArrayAdapter<CItem> adapterMater = new ArrayAdapter<CItem>(myactivity, android.R.layout.simple_spinner_item, lstItm);
drpTask.setAdapter(adapterMater);
}
drpProj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// if (drpMaterial.getCount() > 0)
// drpMaterial.setAdapter(null);
if (!drpProj.getSelectedItem().toString().equals("Select Project"))
{
MaterialAsync MAsync=new MaterialAsync();
MAsync.execute("64");
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//loading task list
Button btnSave=(Button)rootView.findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(), "Saving please wait!", Toast.LENGTH_LONG).show();
Toast.makeText(getActivity().getApplicationContext(), "Saved successfully!", Toast.LENGTH_SHORT).show();
}
});
//getting selected project
drpProj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
CItem itm=(CItem)parent.getItemAtPosition(position);
ProID=itm.getId();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
drpMaterial.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return rootView;
}
答案 0 :(得分:0)
该接口应该在
这样的活动上实现MainActivity extends Activity implements "Interface_Name"{
// and give the actual implementation of method OnFragmentInteractionListener
}
现在您的问题是什么,我猜您不是正确地发布变量,提供更多代码,以便我们能够深入了解并帮助您!
另外转到你的onAttach片段方法并将其评论出来试试看,因为我没有看到你在任何地方使用此回调,这对你也有帮助!
答案 1 :(得分:0)
Fragment generateRequistionFrag=new generate_material_requisition();
这不是获取片段新实例的正确方法,请删除
new generate_material_requisition()
请在Fragment类中创建一个返回新YouFragment实例的方法getInstance()
public static YourFragment getInstance() {
return new Yourfragment;
}
然后当你需要一个新的实例时,只需制作:
YourFragment fragment = YourFragment.getInstance()
答案 2 :(得分:-2)
您需要替换
Fragment generateRequistionFrag=new generate_material_requisition();
的
generate_material_requisition generateRequistionFrag=new generate_material_requisition();