package com.example.criminalintent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.style.TtsSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class CrimeFragment extends Fragment {
private Crime mCrime;
private EditText mTitleField;
private Button mDateButton;
private CheckBox mSolvedChechBox;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCrime=new Crime();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_crime,container,false);
mTitleField=(EditText) v.findViewById(R.id.crime_tittle);
mTitleField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mCrime.setmTitle(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
mDateButton=(Button) v.findViewById(R.id.crime_date) ;
mDateButton.setText(mCrime.getmDate().toString());
mDateButton.setEnabled(false);
mSolvedChechBox=(CheckBox) v.findViewById(R.id.crime_solved);
mSolvedChechBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCrime.setmSolved(isChecked);
}
});
return v;
}
}
داخل MainActivity هم بصورت زیر یک Fragment transactions ایجاد کردم :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = new AppCompatActivity().getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmen_container);
if(fragment==null){
fragment= new CrimeFragment();
fm.beginTransaction().add(R.id.fragmen_container,fragment).commit();
}
}
}
ولی موقع اجرا اکتیویتی از صفحه میپره و از برنامه میاد بیرون .
لاگ کت هم بصورت زیر خروجی میده :
2020-06-04 13:30:41.819 31140-31140/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.criminalintent, PID: 31140
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.criminalintent/com.example.criminalintent.MainActivity}: java.lang.IllegalStateException: Activity has been destroyed
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.IllegalStateException: Activity has been destroyed
at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2114)
at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:637)
at com.example.criminalintent.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:7383)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
2020-06-04 13:30:41.859 31140-31140/? I/Process: Sending signal. PID: 31140 SIG: 9
سوال
akftes 0
با سلام و احترام
من یک کلاس از نوع فرگمنت ایجاد کردم به صورت زیر :
package com.example.criminalintent; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.text.style.TtsSpan; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; public class CrimeFragment extends Fragment { private Crime mCrime; private EditText mTitleField; private Button mDateButton; private CheckBox mSolvedChechBox; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mCrime=new Crime(); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //return super.onCreateView(inflater, container, savedInstanceState); View v = inflater.inflate(R.layout.fragment_crime,container,false); mTitleField=(EditText) v.findViewById(R.id.crime_tittle); mTitleField.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { mCrime.setmTitle(s.toString()); } @Override public void afterTextChanged(Editable s) { } }); mDateButton=(Button) v.findViewById(R.id.crime_date) ; mDateButton.setText(mCrime.getmDate().toString()); mDateButton.setEnabled(false); mSolvedChechBox=(CheckBox) v.findViewById(R.id.crime_solved); mSolvedChechBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mCrime.setmSolved(isChecked); } }); return v; } }
داخل MainActivity هم بصورت زیر یک Fragment transactions ایجاد کردم :
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = new AppCompatActivity().getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmen_container); if(fragment==null){ fragment= new CrimeFragment(); fm.beginTransaction().add(R.id.fragmen_container,fragment).commit(); } } }
ولی موقع اجرا اکتیویتی از صفحه میپره و از برنامه میاد بیرون .
لاگ کت هم بصورت زیر خروجی میده :
2020-06-04 13:30:41.819 31140-31140/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.criminalintent, PID: 31140 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.criminalintent/com.example.criminalintent.MainActivity}: java.lang.IllegalStateException: Activity has been destroyed at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411) at android.app.ActivityThread.-wrap12(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994) at android.os.Handler.dispatchMessage(Handler.java:108) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7529) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) Caused by: java.lang.IllegalStateException: Activity has been destroyed at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2114) at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:683) at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:637) at com.example.criminalintent.MainActivity.onCreate(MainActivity.java:27) at android.app.Activity.performCreate(Activity.java:7383) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411) at android.app.ActivityThread.-wrap12(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994) at android.os.Handler.dispatchMessage(Handler.java:108) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7529) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 2020-06-04 13:30:41.859 31140-31140/? I/Process: Sending signal. PID: 31140 SIG: 9
ممنون میشم کمک کنید .
لینک ارسال
به اشتراک گذاری در سایت های دیگر
3 پاسخ به این سوال تاکنون داده شده است
ارسالهای توصیه شده
بایگانی شده
این موضوع بایگانی و قفل شده و دیگر امکان ارسال پاسخ نیست.