如何在片段中使用intent时解决应用崩溃问题?

时间:2018-05-22 07:36:35

标签: java android android-fragments android-intent nullpointerexception

我使用Viewpager(登录页面和注册页面)设计了一个登录页面,并设置了一个片段,以便在登录后使用打算登录按钮进入下一页时加载到该ViewPager上崩溃

注意:即使用户名和密码中没有任何内容可以测试一切正常,我只是简单地设置了登录按钮以进入下一页

这是我的Java编码:

public class LoginFragment extends Fragment{
    View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
        ViewGroup container, @Nullable Bundle savedInstanceState) {
        final Button b = null;
        b.findViewById(R.id.Loginbutton);
        final EditText e = getView().findViewById(R.id.user);
        EditText e1 = getView().findViewById(R.id.pass);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent l = new Intent(getActivity(), AfterLogin.class);
                startActivity(l);
            }
        });
        view = inflater.inflate(R.layout.fragment_login,container,false);

        return view;
    }

    public LoginFragment() {}
}

没有错误,但在运行时它会导致应用程序崩溃......

这是我的XML:

   <?xml version="1.0" encoding="utf-8"?>
   <android.widget.RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/DefaultWhite">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="125dp"
    android:text="@string/app_name"
    android:textColor="@color/colorPrimaryDark"
    android:textSize="30sp" />
<ImageView
    android:contentDescription="@string/LoginText"
    android:id="@+id/imageView4"
    android:layout_width="48dp"
    android:layout_height="57dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="78dp"
    app:srcCompat="@drawable/accountbox" />

    <EditText
        android:id="@+id/user"
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="@string/LoginText"
        android:gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="300dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/loginpageloginedittext"
        />
    <EditText
        android:id="@+id/pass"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="@string/PasswordText"
        android:gravity="center"
        android:layout_marginEnd="20dp"
        android:layout_marginTop="360sp"
        android:background="@drawable/loginpageedittextpassword"
        android:layout_marginStart="20dp"
        android:inputType="textPassword"
        />

    <Button
        android:id="@+id/LoginButton"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="450dp"
        android:background="@drawable/loginbutton"
        android:layout_marginStart="20dp"
        android:textColor="@color/DefaultWhite"
        android:layout_marginEnd="20dp"
        android:text="@string/LoginButton" />

    <Button
        android:id="@+id/registerbutton"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:background="@drawable/forgotpasswordbutton"
        android:layout_marginTop="450dp"
        android:layout_marginStart="215dp"
        android:layout_marginBottom="10dp"
        android:text="@string/ForgotPassword" />



    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="600dp"
    android:layout_marginStart="160dp"
    android:hint="@string/KnowUsMore" />
   </android.widget.RelativeLayout>

1 个答案:

答案 0 :(得分:0)

避免view.findViewById()而不是getView()我编辑您的代码并在下面给出。核实... 快乐编码; - )

公共类LoginFragment扩展Fragment {

View view;

@Nullable
@Override

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



    view = inflater.inflate(R.layout.fragment_login,container,false);
    return view;


}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

    Button b = view.findViewById(R.id.Loginbutton);
    EditText e = view.findViewById(R.id.user);
    EditText e1 = view.findViewById(R.id.pass);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent l = new Intent(getActivity(), AfterLogin.class);
            startActivity(l);
        }
    });
    super.onViewCreated(view, savedInstanceState);
}

public LoginFragment() {}

}