簡體   English   中英

當我按側邊欄導航上的菜單移動到另一個片段時,突然我的應用程序關閉

[英]when I pressed the menu on the sidebar navigation to move to another fragment, suddenly my application force closes

這是我的mainactivity.java

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @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);
        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.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_pencarian_rute_toko) {
            // Handle the camera action
        } else if (id == R.id.data_toko_prov) {

        } else if (id == R.id.data_toko_kab) {

        } else if (id == R.id.nav_login) {
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction()
                    .add(R.id.FragmentLogin, new LoginFragment())
                    .addToBackStack(null)
                    .commit();
            getSupportActionBar().setTitle("Login Admin");
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

這是myLoginFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends Fragment {


    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_login, container, false);
    }

}

這是我的farment_login.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginFragment"
    android:id="@+id/FragmentLogin"
    android:padding="12dp">

    <!-- TODO: Update blank fragment layout -->
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--TextView Form Login-->
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="6dp"
            android:text="@string/form_login"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/EditText_Username"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <!--EditText Username-->
        <EditText
            android:id="@+id/EditText_Username"
            android:layout_width="279dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:ems="10"
            android:inputType="text|textPersonName"
            android:text="@string/username"
            android:textSize="12sp"
            android:autofillHints="@string/username"
            android:hint="@string/username"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2"
            tools:targetApi="o" />

        <!--EditText Password-->
        <EditText
            android:id="@+id/EditText_Password"
            android:layout_width="279dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:ems="10"
            android:inputType="textPassword"
            android:hint="@string/password"
            android:textSize="12sp"
            android:autofillHints="@string/password"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/EditText_Username"
            tools:targetApi="o" />

        <!--Button Login-->
        <Button
            android:id="@+id/Button_Login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:text="@string/button_login"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/EditText_Password" />

        <!--Button Register-->
        <Button
            android:id="@+id/Button_Register"
            android:layout_width="92dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:textSize="12sp"
            android:text="@string/button_register"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/Button_Cancel"
            app:layout_constraintStart_toEndOf="@+id/Button_Login"
            app:layout_constraintTop_toBottomOf="@+id/EditText_Password" />

        <!--Button Cancel-->
        <Button
            android:id="@+id/Button_Cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:textSize="12sp"
            android:layout_marginBottom="8dp"
            android:text="@string/button_cancel"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

    </android.support.constraint.ConstraintLayout>

</FrameLayout>

這是我的錯誤信息

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.farid.starsmaps, PID: 6513
    java.lang.IllegalArgumentException: No view found for id 0x7f080008 (com.farid.starsmaps:id/FragmentLogin) for fragment LoginFragment{2af8a05 #0 id=0x7f080008}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

當我按下登錄菜單后,我的應用程序將強制關閉。

我已經更改了代碼

FragmentManager fm = getSupportFragmentManager();

成為

FragmentManager fm = getActivity.getSupportFragmentManager();

並且get Activity命令標記為紅色。 並且我已經閱讀了關於一個主題問題的解決方案,並且我的問題在堆棧上溢出,但是結果是相同的,強制關閉

請幫我。

您的ID R.id.FragmentLogin位於fragment_login.xml ,它是LoginFragment本身的布局。 只需在activity_main.xml創建一個FrameLayout並為其指定一個ID,然后使用該ID在MainActivity.java添加LoginFragment.java

像這樣的東西: activity_main.xml

<FrameLayout
    android:id="@+id/login_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

並像這樣添加您的LoginFragment.java

MainActivity.java

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
    .add(R.id.login_container, new LoginFragment())
    .addToBackStack(null)
    .commit();
getSupportActionBar().setTitle("Login Admin");

您需要在Activity中添加Fragment,因此您需要在Activity的布局中創建容器,而不是在Fragment的布局本身中創建容器。

試試看,讓我知道它是否有效;)

該錯誤明確表明fragment_login.xml沒有id。 您應該在fragment_login.xml布局中為loginfragment.java創建一個視圖,並為其指定一個id 使用Mainactivity.javaid添加您的loginfragment.java

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM