当我在可解释的视图中点击图像时,我正试图用另一个片段替换当前的片段。当我点击图像时,应用程序崩溃,而logcat说这是因为活动被破坏了。 / p>
MainActivity.java
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
QuestionsFragment firstFragment = new QuestionsFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
public void thanksFragment(){
//Adicionando código responsável pela transição do
//Fragmento para tela de agradecimento
ThanksFragment thanks = new ThanksFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, thanks);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
QuestionsFragment.Java
public class QuestionsFragment extends Fragment{
MyExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, Emoticons> listDataChild;
//private ArrayAdapter<String> mQuestionAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.questions_fragment, container, false);
//Get the ListView
expListView = (ExpandableListView) rootView.findViewById(R.id.list_view);
//Prepare ListData
prepareListData();
listAdapter = new MyExpandableListAdapter(QuestionsFragment.this.getActivity().getApplicationContext(), listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
// Inflate the layout for this fragment
return rootView;
}
/*
* Preparing the list data
*/
public void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, Emoticons>();
// Adding child data
listDataHeader.add("Rapidez e Eficiência no Check in:");
listDataHeader.add("Rapidez e Eficiência no Check out:");
listDataHeader.add("Cordialidade:");
listDataHeader.add("Limpeza do Apartamento:");
listDataHeader.add("Conforto do Apartamento:");
listDataHeader.add("Artigos de Banho:");
listDataHeader.add("Café da Manha:");
listDataHeader.add("Refeições:");
listDataHeader.add("Serviços de Internet:");
listDataHeader.add("Serviços de Estacionamento:");
// Adding child data
listDataChild.put(listDataHeader.get(0), new Emoticons());
listDataChild.put(listDataHeader.get(1),new Emoticons());
listDataChild.put(listDataHeader.get(2),new Emoticons());
listDataChild.put(listDataHeader.get(3),new Emoticons());
listDataChild.put(listDataHeader.get(4),new Emoticons());
listDataChild.put(listDataHeader.get(5),new Emoticons());
listDataChild.put(listDataHeader.get(6),new Emoticons());
listDataChild.put(listDataHeader.get(7),new Emoticons());
listDataChild.put(listDataHeader.get(8),new Emoticons());
listDataChild.put(listDataHeader.get(9),new Emoticons());
}
}
MyExpandableListAdapter.java
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private static int img1;
private static int img2;
private static int img3;
private static int img4;
private static int img5;
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, Emoticons> _listDataChild;
public MyExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, Emoticons> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.emoticons_question_list_item, null);
}
//Linha Importante, pois instancio um objeto que receberá o valor do Objeto armazenado no HashMap
final Emoticons getQuestionEmoticons = (Emoticons)_listDataChild.get(_listDataHeader.get(groupPosition));
final ImageView imgBad = (ImageView) convertView.findViewById(R.id.image_bad);
imgBad.setImageResource(getQuestionEmoticons.getImgBad());
final ImageView imgBad2 = (ImageView) convertView.findViewById(R.id.image_bad2);
imgBad2.setImageResource(getQuestionEmoticons.getImgBad2());
final ImageView imgNormal = (ImageView) convertView.findViewById(R.id.image_normal);
imgNormal.setImageResource(getQuestionEmoticons.getImgNormal());
final ImageView imgGood = (ImageView) convertView.findViewById(R.id.image_good);
imgGood.setImageResource(getQuestionEmoticons.getImgGood());
final ImageView imgExcelent = (ImageView) convertView.findViewById(R.id.image_excelent);
imgExcelent.setImageResource(getQuestionEmoticons.getImgExcelent());
//Aqui foram adicionados os listners
imgBad.setClickable(true);
imgBad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getQuestionEmoticons.changeEmoticons(1);
imgBad.setImageResource(getQuestionEmoticons.getImgBad());
imgBad2.setImageResource(getQuestionEmoticons.getImgBad2());
imgNormal.setImageResource(getQuestionEmoticons.getImgNormal());
imgGood.setImageResource(getQuestionEmoticons.getImgGood());
imgExcelent.setImageResource(getQuestionEmoticons.getImgExcelent());
MainActivity main = new MainActivity();
main.thanksFragment();
}
});
imgBad2.setClickable(true);
imgBad2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getQuestionEmoticons.changeEmoticons(2);
imgBad.setImageResource(getQuestionEmoticons.getImgBad());
imgBad2.setImageResource(getQuestionEmoticons.getImgBad2());
imgNormal.setImageResource(getQuestionEmoticons.getImgNormal());
imgGood.setImageResource(getQuestionEmoticons.getImgGood());
imgExcelent.setImageResource(getQuestionEmoticons.getImgExcelent());
}
});
imgNormal.setClickable(true);
imgNormal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getQuestionEmoticons.changeEmoticons(3);
imgBad.setImageResource(getQuestionEmoticons.getImgBad());
imgBad2.setImageResource(getQuestionEmoticons.getImgBad2());
imgNormal.setImageResource(getQuestionEmoticons.getImgNormal());
imgGood.setImageResource(getQuestionEmoticons.getImgGood());
imgExcelent.setImageResource(getQuestionEmoticons.getImgExcelent());
}
});
imgGood.setClickable(true);
imgGood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getQuestionEmoticons.changeEmoticons(4);
imgBad.setImageResource(getQuestionEmoticons.getImgBad());
imgBad2.setImageResource(getQuestionEmoticons.getImgBad2());
imgNormal.setImageResource(getQuestionEmoticons.getImgNormal());
imgGood.setImageResource(getQuestionEmoticons.getImgGood());
imgExcelent.setImageResource(getQuestionEmoticons.getImgExcelent());
}
});
imgExcelent.setClickable(true);
imgExcelent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getQuestionEmoticons.changeEmoticons(5);
imgBad.setImageResource(getQuestionEmoticons.getImgBad());
imgBad2.setImageResource(getQuestionEmoticons.getImgBad2());
imgNormal.setImageResource(getQuestionEmoticons.getImgNormal());
imgGood.setImageResource(getQuestionEmoticons.getImgGood());
imgExcelent.setImageResource(getQuestionEmoticons.getImgExcelent());
}
});
return convertView;
}
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.question_list_item, null);
}
TextView txtListGroup = (TextView) convertView
.findViewById(R.id.question_list_item_textview);
txtListGroup.setText(headerTitle);
return convertView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这是我得到的错误:
FATAL EXCEPTION: main
Process: br.ind.uptech.princessfeedback, PID: 6631
java.lang.IllegalStateException: Activity has been destroyed
at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
at android.app.BackStackRecord.commitInternal(BackStackRecord.java:729)
at android.app.BackStackRecord.commit(BackStackRecord.java:705)
at br.ind.uptech.princessfeedback.MainActivity.thanksFragment(MainActivity.java:54)
at
br.ind.uptech.princessfeedback.MyExpandableListAdapter$1.onClick(MyExpandableListAdapter.java:96)
at android.view.View.performClick(View.java:5242)
at android.view.View$PerformClick.run(View.java:21156)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6862)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
非常感谢!
答案 0 :(得分:0)
我认为问题出在这里。你不应该自己新的MainActivity。如果要从MyExpandableListAdapter调用MainActivity中的方法,可以将活动引用传递给它。为安全起见,您可以在QuestionsFragment中使用回调。
//Aqui foram adicionados os listners
imgBad.setClickable(true);
imgBad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getQuestionEmoticons.changeEmoticons(1);
imgBad.setImageResource(getQuestionEmoticons.getImgBad());
imgBad2.setImageResource(getQuestionEmoticons.getImgBad2());
imgNormal.setImageResource(getQuestionEmoticons.getImgNormal());
imgGood.setImageResource(getQuestionEmoticons.getImgGood());
imgExcelent.setImageResource(getQuestionEmoticons.getImgExcelent());
MainActivity main = new MainActivity();
main.thanksFragment();
}
});