java如何解决在片段中使用意图时应用程序崩溃的问题?
我已经使用Viewpager(登录页面和注册页面)设计了一个登录页面,并设置了一个片段,当登录崩溃后,使用intentive for login按钮进入下一页时,该片段将加载到该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"?>
<安卓.widget.RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:background="@color/DefaultWhite">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentTop="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="125dp"
安卓:text="@string/app_name"
安卓:textColor="@color/colorPrimaryDark"
安卓:textSize="30sp" />
<ImageView
安卓:contentDescription="@string/LoginText"
安卓:id="@+id/imageView4"
安卓:layout_width="48dp"
安卓:layout_height="57dp"
安卓:layout_alignParentTop="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="78dp"
app:srcCompat="@drawable/accountbox" />
<EditText
安卓:id="@+id/user"
安卓:inputType="text"
安卓:layout_width="match_parent"
安卓:layout_height="50dp"
安卓:hint="@string/LoginText"
安卓:gravity="center"
安卓:layout_marginLeft="20dp"
安卓:layout_marginTop="300dp"
安卓:layout_marginRight="20dp"
安卓:background="@drawable/loginpageloginedittext"
/>
<EditText
安卓:id="@+id/pass"
安卓:layout_width="match_parent"
安卓:layout_height="50dp"
安卓:hint="@string/PasswordText"
安卓:gravity="center"
安卓:layout_marginEnd="20dp"
安卓:layout_marginTop="360sp"
安卓:background="@drawable/loginpageedittextpassword"
安卓:layout_marginStart="20dp"
安卓:inputType="textPassword"
/>
<Button
安卓:id="@+id/LoginButton"
安卓:layout_width="175dp"
安卓:layout_height="wrap_content"
安卓:layout_alignParentTop="true"
安卓:layout_marginTop="450dp"
安卓:background="@drawable/loginbutton"
安卓:layout_marginStart="20dp"
安卓:textColor="@color/DefaultWhite"
安卓:layout_marginEnd="20dp"
安卓:text="@string/LoginButton" />
<Button
安卓:id="@+id/registerbutton"
安卓:layout_width="175dp"
安卓:layout_height="wrap_content"
安卓:background="@drawable/forgotpasswordbutton"
安卓:layout_marginTop="450dp"
安卓:layout_marginStart="215dp"
安卓:layout_marginBottom="10dp"
安卓:text="@string/ForgotPassword" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="600dp"
安卓:layout_marginStart="160dp"
安卓:hint="@string/KnowUsMore" />
</安卓.widget.RelativeLayout>
# 1 楼答案
避免
view.findViewById()
而不是getView()
我编辑了您的代码,如下所示。检查一下。。。 快乐编码;-)公共类LoginFragment扩展了片段{
}