开始新活动会导致Android应用崩溃

时间:2016-05-02 20:34:46

标签: java android xml

我已经阅读过有关此主题的几个问题,但似乎没有任何问题可以解决我的问题。当我开始新的Android活动时,应用程序崩溃了。我怀疑它在我的布局文件中。自Android开始包含内容xml文件以来,我遇到了这个问题。

这是我开始新活动的地方:

private void logUserIn(User returnedUser){
    userLocalStore.setUserLoggedIn(true);
    userLocalStore.storeUserData(returnedUser);
    startActivity(new Intent(this, MainActivity.class));
}

这是我在MainActivity.class中找到的OnCreate代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bJoinLeague = (Button)findViewById(R.id.bJoinLeague);
    bJoinLeague.setOnClickListener(this);

    bCreateLeague = (Button)findViewById(R.id.bCreateLeague);
    bCreateLeague.setOnClickListener(this);

    bViewLeagues = (Button)findViewById(R.id.bViewLeagues);
    bViewLeagues.setOnClickListener(this);

    bLogout = (Button) findViewById(R.id.bLogout);
    bLogout.setOnClickListener(this);

    userLocalStore = new UserLocalStore(this);
}

我怀疑问题出在我的布局文件中,但由于我是Android开发的新手,我不确定。这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.myname.myapp.MainActivity">

<include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

我的content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_width="match_parent">

<Button
    android:id="@+id/bCreateLeagues"
    android:text="Create League"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/bJoinLeague"
    android:text="Join League"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/bViewLeagues"
    android:text="View My Leagues"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/bLogout"
    android:text="Logout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

我还在我的Manifest.xml文件中包含了这些新活动,我已经将其视为几个类似问题的解决方案。这是Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myname.myapp">

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Login" />
    <activity android:name=".Register" />
    <activity
        android:name=".CreateLeague"
        android:label="@string/title_activity_create_league"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".JoinLeague"
        android:label="@string/title_activity_join_league"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".ViewLeagues"
        android:label="@string/title_activity_view_leagues"
        android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

</manifest>

最后,我的logcat

FATAL EXCEPTION: main
Process: com.example.myname.myapp, PID: 3931
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myname.myapp/com.example.myname.myapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.myname.myapp.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

正如我之前提到的,我认为错误在布局文件中。自API23以来,这已成为一个问题,其中Android Studio将content.xml和activity.xml分开。

1 个答案:

答案 0 :(得分:4)

它看起来是由一个错字引起的。

在java代码中,您正在抓取一个ID为bCreateLeague的按钮,但在XML中,会创建一个ID为bCreateLeagues的按钮(注意结尾处的“s”)