简体   繁体   中英

Android app crashes upon replacing fragment:

I followed the tutorial here on how to make Fragment Transactions but whenever I hit my button to transact the fragment my app crashes. My code:

public class MainActivity extends Activity {

    public FragmentTransaction transaction = getFragmentManager().beginTransaction();
    public Fragment loginFragment = new LoginFragment();
    public Fragment mainFragment = new MainFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {

            getFragmentManager().beginTransaction().add(R.id.container, loginFragment).commit();

        } else {

            getFragmentManager().beginTransaction().add(R.id.container, mainFragment).commit();

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    public void login() {

        transaction.replace(R.id.container, mainFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public static class LoginFragment extends Fragment {

        public LoginFragment() {

        }

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

            View rootView = inflater.inflate(R.layout.fragment_login, container, false);

            return rootView;
        }
    }

    public static class MainFragment extends Fragment {

        public MainFragment() {

        }

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

            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            return rootView;
        }
    }
}

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.pointlight.kingdomcraft.MainActivity$LoginFragment" >

    <Button
        android:id="@+id/button_submit"
        android:onClick="login" />

</RelativeLayout>

So in conclusion The submit button on the login fragment replaces the login fragment with the main fragment, or so it should but instead it crashes.

To narrow it down, in the xml I set the button to android:onCLick= "login" which in turn calls the login method inside my activity class:

public void login() {

            transaction.replace(R.id.container, mainFragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }

What have I done wrong, I've been working on fragments transactions for two days to no justice.

Logcat:

?:??: W/?(?): --------- beginning of /dev/log/main

?:??: W/?(?): --------- beginning of /dev/log/system

EDIT:

If I debug and hit the submit button instead of crashing the button gets highlighted blue and the app freezes

The signature for your onClick handler android:onClick="login" is wrong:

public void login() {

should be

public void login(View v) {

android:onClick attribute gets translated to code that looks up the handler by method name using reflection. The lookup process also requires method signature ie parameter types. If the signature doesn't match, the method cannot be found and an exception is thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM