如何解决碎片错误

时间:2017-02-22 13:08:39

标签: android android-fragments

以下是我的代码。

MainActivity.java 代码如下:

public class Login extends AppCompatActivity {
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportFragmentManager().beginTransaction().replace(R.id.loginFrame, new LoginFragment()).commit();

    }
}
MainActivity.java

中调用的

片段活动 .java代码

  public class LoginFragment extends Fragment {
        EditText emailEt,passwordEt;
        String email,password;
        Button login,signup;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View rootview = super.onCreateView(inflater, container, savedInstanceState);

            emailEt=(EditText)rootview.findViewById(R.id.emailEt);
            passwordEt=(EditText)rootview.findViewById(R.id.passEt);
            login=(Button)rootview.findViewById(R.id.loginBtn);
            signup=(Button)rootview.findViewById(R.id.signupBtn);

            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    email=emailEt.getText().toString();
                    password=passwordEt.getText().toString();

                    if(email.length()==0){
                        emailEt.requestFocus();
                        emailEt.setError("FIELD CANNOT BE EMPTY");
                    }else if(password.length()==0){
                        passwordEt.requestFocus();
                        passwordEt.setError("FIELD CANNOT BE EMPTY");
                    }else{
                        Toast.makeText(getActivity(),"Validation Done",Toast.LENGTH_SHORT).show();
                        Intent in=new Intent(getActivity(),Options_class.class);
                        startActivity(in);
                    }
                }
            });

            signup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(getActivity(),Signup.class);
                    startActivity(intent);
                }
            });
            return rootview;
        }
    }

错误 logcat 的代码显示oncreate()mainclass中的错误

  

t dalvik.system.VMRuntime.newNonMovableArray(原生方法)                                                                                  在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)                                                                                  在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)                                                                                  在   android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)                                                                                  在   android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1152)                                                                                  在   android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:724)                                                                                  在   android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:575)                                                                                  在android.content.res.Resources.loadDrawable(Resources.java:854)                                                                                  在android.content.res.TypedArray.getDrawable(TypedArray.java:928)                                                                                  在android.widget.ImageView。(ImageView.java:167)                                                                                  在android.widget.ImageView。(ImageView.java:155)                                                                                  在   android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:60)                                                                                  在   android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:56)                                                                                  在   android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)                                                                                  在   android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021)                                                                                  在   android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080)                                                                                  在   android.support.v4.view.LayoutInflaterCompatHC $ FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)                                                                                  在   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)                                                                                  在   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)                                                                                  在android.view.LayoutInflater.rInflate(LayoutInflater.java:858)                                                                                  在   android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)                                                                                  在android.view.LayoutInflater.inflate(LayoutInflater.java:518)                                                                                  在android.view.LayoutInflater.inflate(LayoutInflater.java:426)                                                                                  在android.view.LayoutInflater.inflate(LayoutInflater.java:377)                                                                                  在   android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)                                                                                  在   android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)                                                                                  在com.example.nirmit.demo_frg.Login.onCreate(Login.java:13)                                                                                  在android.app.Activity.performCreate(Activity.java:6672)                                                                                  在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)                                                                                  在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)                                                                                  在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)                                                                                  在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                  在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1473)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                  在android.os.Looper.loop(Looper.java:154)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:6123)                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                  在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

任何正文都可以帮我解决mainActivity和片段代码中的logcat问题。

2 个答案:

答案 0 :(得分:1)

修改此行

View rootview = super.onCreateView(inflater, container, savedInstanceState);

进入这个

   View rootview = inflater.inflate(R.layout.fragmentlayout, container, false);

答案 1 :(得分:0)

 public class Login extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

            Bundle bundle = new Bundle();
            fragment = new LoginFragment();
            fragment.setArguments(bundle);

            if (fragment != null) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.loginFrame, fragment);
                fragmentTransaction.commit();
                //initToolbar();
            }


        }
    }

public class LoginFragment extends Fragment {
    View rootview;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(your_layout, null);


        return rootview;
    }
}