新活动的空白屏幕 - Android

时间:2016-05-03 08:14:18

标签: java android xml

我试图通过点击按钮开始新活动,但它只显示为空白屏幕。没有XML元素显示。有趣的是,每当我按下手机的“后退”按钮时,我都会收到一条消息,说明“不幸的是,已停止工作”。在此对话框上单击“确定”后,将显示实际布局。

这是我在OnClick方法中启动新活动的地方:

public void onClick(View v) {
    switch(v.getId()){
        case R.id.bLogout:
            userLocalStore.clearUserData();
            userLocalStore.setUserLoggedIn(false);
            startActivity(new Intent(MainActivity.this, Login.class));
        case R.id.bCreateLeague:
            startActivity(new Intent(MainActivity.this, CreateLeague.class));
        case R.id.bJoinLeague:
            startActivity(new Intent(MainActivity.this, JoinLeague.class));
        case R.id.bViewLeagues:
            startActivity(new Intent(MainActivity.this, ViewLeagues.class));
}

这是CreateLeague.class中的OnCreate代码:

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

    //create variables for all xml objects
    etLeagueName = (EditText)findViewById(R.id.etLeagueName);
    etLeaguePassword = (EditText)findViewById(R.id.etLeaguePassword);
    bCreateLeague = (Button)findViewById(R.id.bCreateLeague);
    tvGoBack = (TextView)findViewById(R.id.tvGoBack);

    //set OnClickListeners
    bCreateLeague.setOnClickListener(this);
    tvGoBack.setOnClickListener(this);
}

这是我的content_create_league.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">



<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TextView
            android:layout_width="wrap_content"
            android:text="League Name"
            android:layout_height="wrap_content"/>

        <EditText
            android:id="@+id/etLeagueName"
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:text="Create a password"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/etLeaguePassword"
            android:layout_width="match_parent"
            android:inputType="textPassword"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:text="Re-type Password"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/etReTypeLeaguePassword"
            android:layout_width="match_parent"
            android:inputType="textPassword"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/bCreateLeague"
            android:text="Create League"
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tvGoBack"
            android:layout_width="wrap_content"
            android:text="Go Back"
            android:textStyle="bold"
            android:layout_gravity="center_horizontal"
            android:layout_height="wrap_content" />

    </LinearLayout>
</ScrollView>

我的activity_create_league.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.CreateLeague">

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

我的AndroidManifest.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" />
    <intent-filter>
        <action android:name="com.example.myname.myapp.CreateLeague" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

    <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>

最后,这是我的程序在按下手机的后退按钮后崩溃时的logcat:

FATAL EXCEPTION: main
 Process: com.example.myname.myapp, PID: 15584
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myname.myapp/com.example.myname.myapp.JoinLeague}: 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.JoinLeague.onCreate(JoinLeague.java:28)
 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) 

不确定这是多么相关,但我注意到当我在调试模式中单步执行代码时,CreateLeague.java代码中savedInstanceState的值为null。

单击相关按钮时如何显示XML内容?我已经看过几个教程,据我所知,我正在做正确的事情。另外,为什么按下后退按钮时会显示布局?任何解决方案和/或想法都表示赞赏。

1 个答案:

答案 0 :(得分:0)

setContentView(R.layout.activity_create_league);