OptionsMenu Item ni bosish bilan parchani o'zgartiring

men ilovamning versiyasini Fragmentdan foydalanib o'zgartirishga harakat qilaman. Men bir necha kundan buyon harakat qildim, shuning uchun muammomni taqdim etish uchun toza kodni ishlab chiqdim. OptionsMenu bilan MainActivity bor, ushbu OptionsMenu menda "action_connect" deb nomlangan element bor. Mening ilovamning birinchi ko'rinishi toza sahifa bo'lishi kerak (hech narsa yo'q). Agar biror kishi "action_connect" elementini chertib yuborsa, View menda TextView bo'lgan Fragmentga o'zgartirishni xohlayman. Joriy kod bilan bog'liq muammolar: Ilova elementni bosish bilan o'chib ketadi.

Umid qilamanki, men buni hal qilishga yordam bera olaman. Oldindan rahmat.

Mening kodim:

Asosiy faoliyat:

public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   //Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   //Handle action bar item clicks here. The action bar will
   //automatically handle clicks on the Home/Up button, so long
   //as you specify a parent activity in AndroidManifest.xml.

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    MainActivityFragment mMainActivityFragment = new MainActivityFragment();

    int id = item.getItemId();

    if (id == R.id.action_connect) {

        ft.replace(R.id.default_container, mMainActivityFragment);
    }


    ft.commit();
    return super.onOptionsItemSelected(item);

}

Fragment:

public class MainActivityFragment extends Fragment{

@Override
public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);



}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){

    //get command buttons
    View rootView = inflater.inflate(R.layout.fragment_connect, parent, false);

    return rootView;
}

}

Fragment-XML:




Activity-XML:




Mana, jurnallar, uning to'g'ri bo'lganiga umid qilamiz:

02-27 12:44:44.473 5513-5513/? I/art: Not late-enabling -Xcheck:jni (already on)
02-27 12:44:44.475 5513-5513/? W/art: Unexpected CPU variant for X86 using defaults: x86
02-27 12:44:44.530 5513-5513/hsulm.cermitbasmati_2.app W/System: ClassLoader referenced unknown path: /data/app/hsulm.cermitbasmati_2.app-1/lib/x86
02-27 12:44:44.544 5513-5513/hsulm.cermitbasmati_2.app I/InstantRun: Instant Run Runtime started. Android package is hsulm.cermitbasmati_2.app, real application class is null. [ 02-27 12:44:44.544  1560: 1587 D/         ]HostConnection::get() New Host Connection established 0x98e23700, tid 1587
02-27 12:44:45.850 5513-5513/hsulm.cermitbasmati_2.app W/System: ClassLoader referenced unknown path: /data/app/hsulm.cermitbasmati_2.app-1/lib/x86
02-27 12:44:45.938 5513-5513/hsulm.cermitbasmati_2.app W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-27 12:44:46.028 5513-5557/hsulm.cermitbasmati_2.app I/OpenGLRenderer: Initialized EGL, version 1.4
02-27 12:44:46.028 5513-5557/hsulm.cermitbasmati_2.app D/OpenGLRenderer: Swap behavior 1
02-27 12:44:46.159 5513-5557/hsulm.cermitbasmati_2.app E/EGL_emulation: tid 5557: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
02-27 12:44:46.159 5513-5557/hsulm.cermitbasmati_2.app W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x99e9ce20, error=EGL_BAD_MATCH
02-27 12:44:48.028 5513-5513/hsulm.cermitbasmati_2.app W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
02-27 12:44:48.105 5513-5557/hsulm.cermitbasmati_2.app E/EGL_emulation: tid 5557: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
02-27 12:44:48.105 5513-5557/hsulm.cermitbasmati_2.app W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x99287620, error=EGL_BAD_MATCH
02-27 12:44:49.497 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager: No view found for id 0x7f0b0057 (hsulm.cermitbasmati_2.app:id/default_container) for fragment MainActivityFragment{1d6183d #0 id=0x7f0b0057}
02-27 12:44:49.498 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager: Activity state:
02-27 12:44:49.498 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:   Local FragmentActivity 5db9396 State:
02-27 12:44:49.498 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:     mCreated=truemResumed=true mStopped=false mReallyStopped=false
02-27 12:44:49.498 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:     mLoadersStarted=true
02-27 12:44:49.498 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:   FragmentManager misc state:
02-27 12:44:49.498 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:     [email protected]832
02-27 12:44:49.498 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:     m[email protected]fb91832
02-27 12:44:49.499 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:     mCurState=5 mStateSaved=false mDestroyed=false
02-27 12:44:49.499 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:   View Hierarchy:
02-27 12:44:49.499 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:     com.android.internal.policy.DecorView{fdab983 V.E..... ... 0,0-1080,1920}
02-27 12:44:49.499 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:       android.widget.LinearLayout{2f91700 V.E..... ... 0,0-1080,1776}
02-27 12:44:49.499 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:         android.view.ViewStub{310fb39 G.E..... ... 0,0-0,0 #10203ef android:id/action_mode_bar_stub}
02-27 12:44:49.499 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:         android.widget.FrameLayout{1dc857e V.E..... ... 0,72-1080,1776}
02-27 12:44:49.499 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:           android.support.v7.widget.ActionBarOverlayLayout{9056adf V.E..... ... 0,0-1080,1704 #7f0b0045 app:id/decor_content_parent}
02-27 12:44:49.499 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:             android.support.v7.widget.ContentFrameLayout{558a32c V.E..... ... 0,168-1080,1704 #1020002 android:id/content}
02-27 12:44:49.499 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:             
02-27 12:44:49.499 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager: android.support.v7.widget.ActionBarContainer{87d11f5 V.ED.... ... 0,0-1080,168 #7f0b0046 app:id/action_bar_container}
02-27 12:44:49.500 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:               android.support.v7.widget.Toolbar{1377b8a V.E..... ... 0,0-1080,168 #7f0b0047 app:id/action_bar}
02-27 12:44:49.500 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:                 android.support.v7.widget.AppCompatTextView{2ffd5fb V.ED.... ... 48,43-544,124}
02-27 12:44:49.500 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:                 android.support.v7.widget.ActionMenuView{542c618 V.E..... ... 960,0-1080,168}
02-27 12:44:49.500 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:                   android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton{459d871 VFED..CL ... 0,12-120,156}
02-27 12:44:49.500 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:               android.support.v7.widget.ActionBarContextView{299c656 G.E..... ... 0,0-0,0 #7f0b0048 app:id/action_context_bar}
02-27 12:44:49.500 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:       android.view.View{26bd6d7 V.ED.... ... 0,1776-1080,1920 #1020030 android:id/navigationBarBackground}
02-27 12:44:49.500 5513-5513/hsulm.cermitbasmati_2.app E/FragmentManager:       android.view.View{4aa2bc4 V.ED.... ... 0,0-1080,72 #102002f android:id/statusBarBackground}
02-27 12:44:49.501 5513-5513/hsulm.cermitbasmati_2.app D/AndroidRuntime: Shutting down VM
02-27 12:44:49.501 5513-5513/hsulm.cermitbasmati_2.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: hsulm.cermitbasmati_2.app, PID: 5513
                                                                         java.lang.IllegalArgumentException: No view found for id 0x7f0b0057 (hsulm.cermitbasmati_2.app:id/default_container) for fragment MainActivityFragment{1d6183d #0 id=0x7f0b0057}
                                                                             at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:987)
                                                                             at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
                                                                             at android.app.BackStackRecord.run(BackStackRecord.java:816)
                                                                             at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
                                                                             at android.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
                                                                             at android.os.Handler.handleCallback(Handler.java:751)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
2
setContentView (R.layout.xml) ni o'tkazib yuborgan deb o'ylayman. qo'shing va tekshiring.
qo'shib qo'ydi muallif Sai Kiran, manba
Iltimos, loglarni qo'shing. Ilova nima uchun buzilayotganini bilishga yordam beradi.
qo'shib qo'ydi muallif Passiondroid, manba
Tashakkur! @ Sai Kiran, buni sen hal qilding!
qo'shib qo'ydi muallif keko, manba
Buni shunday qildim: @Oncreate public void onCreate (Bundle savedInstance) {super.onCreate (savedInstance); setContentView (R.layout.main_activity); }
qo'shib qo'ydi muallif keko, manba
qaerga qo'yish kerak?
qo'shib qo'ydi muallif keko, manba

6 javoblar

Sizning muammoingizni hozir ko'rib turibman, deb o'ylayman, siz ushbu kodni MainActivity sinfiga qo'yib yubormadingiz deb o'ylaysiz:

@Override
public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    setContentView(R.layout.your_activity_layout);
}

almashtirish usuli yordamida Fragment kodini o'zgartirganda, setContentView() usulidan foydalaning, shunda siz ID kodining idiga kirishingiz mumkin. FrameLayout sizning holatingizda o'zgartirasiz: default_container .

1
qo'shib qo'ydi
Do'stim, men yordam berish uchun keldim
qo'shib qo'ydi muallif blueware, manba
Yaxshi, siz buni keyinroq qilishingiz mumkin, ajoyib kun :)
qo'shib qo'ydi muallif blueware, manba
Men bunga yordam berganimga xursandman. Agar javobim sizga yordam bergan bo'lsa, siz uni ovoz berishni yoqtirasiz. Rahmat
qo'shib qo'ydi muallif blueware, manba
Rahmat, men uni sog'indim.
qo'shib qo'ydi muallif keko, manba
Kechirasiz, buni qila olmayman, mening obro'yim juda kam, men imning yangi a'zosi sifatida :-(
qo'shib qo'ydi muallif keko, manba
Men ishonch hosil qilaman! juda ajoyib kun ham bor!
qo'shib qo'ydi muallif keko, manba
U erga bor! Yana javob uchun rahmat!
qo'shib qo'ydi muallif keko, manba

Sizning OnOptionsItemSelectedda MainActivityFragment-ni quyidagi tarzda chaqiring:

 if (id == R.id.action_connect) {
     FragmentManager fm = getFragmentManager();
     fm.beginTransaction().replace(R.id.default_container, new MainActivityFragment(), "Tag").commit();
 }

Umid qilamanki bu yordam.

0
qo'shib qo'ydi
Qanday xatoga yo'l olasiz? iltimos, logcatni savolingizga qo'shing.
qo'shib qo'ydi muallif tahsinRupam, manba
Hali ham qulab tushdi :-(
qo'shib qo'ydi muallif keko, manba
mening savolim oxirida logcat qo'shdi
qo'shib qo'ydi muallif keko, manba

use this this will help you removeft.replace(R.id.default_container, mMainActivityFragment); add with inif (id == R.id.action_connect) { }

  Fragment fragment = new MainActivityFragment;
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.default_container, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
0
qo'shib qo'ydi
faqat oft o'chirish
qo'shib qo'ydi muallif Akash pasupathi, manba
FragmentManager fragmentManager = getSupportFragmentManager ();
qo'shib qo'ydi muallif Akash pasupathi, manba
"GetActivity ()" uchun xato qilaman, bu usulni hal qila olmasligini aytadi. fikr bormi?
qo'shib qo'ydi muallif keko, manba

buni o'zgartirish

ft.replace(R.id.default_container, mMainActivityFragment);

uchun

ft.replace(R.id.default_container, mMainActivityFragment).commit();
0
qo'shib qo'ydi
kodingizdan ft.commit (); ni olib tashlang
qo'shib qo'ydi muallif Redman, manba
getSupportFragmentManager() funktsiyasidan foydalaning;
qo'shib qo'ydi muallif Aditya Desai, manba
Sizning qaroringizni amalga oshirishimdan oldin bu haqda gapirib berdim
qo'shib qo'ydi muallif keko, manba
Xuddi shu muammo, uzr :-(
qo'shib qo'ydi muallif keko, manba

Birinchidan, support.v4 fragmentlaridan foydalanganingizga ishonch hosil qiling, keyin buni sinab ko'ring.

private void setUpFragment(SomeFragment fragment){
FragmentManger mFragmentManager = getSupportFragmentManager();

mFragmentManager.beginTransaction()
    .replace(R.id.default_container, fragment, "FrgamentTAG")
    .addToBackStack(null)
    .commitAllowingStateLoss();

mFragmentManager.executePendingTransactions();
}
0
qo'shib qo'ydi

O'ylaymanki, bu muammoni hal qiladi, deb umid qilaman

            Fragment mMainActivityFragment = null;

            mMainActivityFragment = new MainActivityFragment();
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.default_container, mMainActivityFragment);
            ft.commit();
0
qo'shib qo'ydi