我正在开发一个演示应用程序,当我登录它将导航到另一个页面。
此处使用按钮我正试图导航到其他页面
主要活动
public class WelcomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_welcome);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button ibsignin = (Button) findViewById(R.id.ibsignin);
ibsignin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (v.getContext(), Register.class);
startActivity(intent);
}
});
}
}
我在xml文件中定义了按钮代码,但我仍然得到空指针异常。
XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.nandan.uschedule.WelcomeActivity"
android:background="@drawable/edit1">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:id="@+id/welcome"
android:background="@drawable/minnesota"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:clickable="false"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:id="@+id/ibusername"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_marginBottom="200dp"
android:layout_marginTop="225dp"
android:inputType="textEmailAddress|text"
android:hint="User name/ Email Address"
android:textColor="#000000"
android:textStyle="normal" />
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:inputType="textPassword|text"
android:ems="10"
android:id="@+id/ibpassword"
android:hint="Password"
android:layout_gravity="center"
android:layout_marginTop="-200dp"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember me"
android:id="@+id/cbRememberme"
android:hint="Remember me"
android:layout_gravity="center" />
<Button
android:layout_width="250dp"
android:layout_height="38dp"
android:text="Sign in"
android:id="@+id/ibsingin"
android:layout_gravity="center"
android:background="#4D306A" />
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Do not have an account yet?"
android:id="@+id/havenoaccount"
android:layout_gravity="center"
android:textColor="#000000"
android:paddingTop="12dp"
android:textSize="20dp" />
<Button
android:layout_width="250dp"
android:layout_height="38dp"
android:text="Sign up"
android:id="@+id/ibsignup"
android:layout_gravity="center"
android:background="#4D306A" />
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Forgot Password?"
android:id="@+id/ibforgotpwd"
android:layout_gravity="center"
android:textColor="#000000"
android:textSize="20dp"
android:clickable="true" />
</LinearLayout>
</RelativeLayout>
我的logcat
Process: com.example.nandan.uschedule, PID: 2487
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nandan.uschedule/com.example.nandan.uschedule.WelcomeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.nandan.uschedule.WelcomeActivity.onCreate(WelcomeActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
我不知道哪一行是27,但最有可能的是NPE来自这里:
Button ibsignin = (Button) findViewById(R.id.ibsignin);
确保您的按钮位于XML布局中,并且称为ibsignin。