简体   繁体   English

替换片段后,Android应用程序崩溃:

[英]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: 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. 因此,总而言之,登录片段上的Submit按钮将登录片段替换为主片段,或者应该这样,但是它崩溃了。

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: 为了缩小范围,我在xml中将按钮设置为android:onCLick= "login" ,这又在我的活动类中调用了登录方法:

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: logcat的:

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

?:??: W/?(?): --------- beginning of /dev/log/system ?:??:W /?(?):--------- / 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: 您的onClick处理程序android:onClick="login"的签名错误:

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. android:onClick属性被转换为使用反射通过方法名称查找处理程序的代码。 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. 如果签名不匹配,则找不到该方法,并引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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