片段TextView ID导致崩溃

时间:2014-12-04 17:32:32

标签: java android android-layout android-fragments android-studio

我主要是在寻求澄清和建议。在我正在开发的应用程序中,我设置了一个用于处理一些基本控件和文本的片段,并将必要的XML从活动布局文件移动到片段布局文件中,而不进行任何更改。但是现在当我运行应用程序时,我得到一个NULLPOINTEREXCEPTION,当我尝试通过其ID设置textView时,它似乎是在我的GameStart活动的第19行引起的。我没有更改ID,只将其移动到片段布局文件,所以我有两个问题:

1)究竟发生了什么原因,因为在我解决之前发生了类似的事情,但我没有看到它在Android开发者的参考文档中发生的原因

2)有关如何解决此问题的任何建议

下面是GameStart活动,包含错误的LogCat以及有问题的XML文件。在此先感谢,我花了2个小时试图找到这个答案。

以下编辑文件/ LOGCAT

logcat的:

12-04 13:48:06.322      826-826/com.saphiric.simproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.saphiric.simproject/com.saphiric.simproject.GameStart}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
            at com.saphiric.simproject.GameStart.<init>(GameStart.java:25)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1130)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

启动活动(当应用尝试启动GameStart活动时发生错误):

package com.saphiric.simproject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;


public class TitleMenu extends ActionBarActivity {

    public void startGame(View view){
        Intent gameStart = new Intent(this, GameStart.class);
        startActivity(gameStart);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        setContentView(R.layout.activity_main_menu);
    }
}

片段XML

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Relative layout to handle main interaction area -->
    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/bg_text">

        <TextView
            android:id="@id/storyText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:text="@string/story_opening_one"
            android:textColor="@color/black" />

        <Button
            android:id="@+id/advanceButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/controlsFragment"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="@drawable/button_advance"
            android:onClick="advanceGame" />

        <Button
            android:id="@+id/menuButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@id/advanceButton"
            android:background="@drawable/menu_button" />
    </RelativeLayout>
</RelativeLayout>

片段类:

package com.saphiric.simproject;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Saphiric on 12/4/14.
 */
public class ControlsFragment extends Fragment {

    /**
     * Fragment Lifecycle Methods
     */

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        // Inflates the layout resource for the fragment
        return inflater.inflate(R.layout.controls_fragment, container, false);
    }

    @Override
    public void onPause(){

    }
}

GameStart活动:

    package com.saphiric.simproject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

/**
 * Created by Saphiric on 11/12/14.
 */
public class GameStart extends Activity{

    /**
     * Necessary activity variables
     */
    protected int clickCount = 0;
    TextView storyText = (TextView) findViewById(R.id.storyText);

    /**
     * Sets up and alert dialogue builder for making decisions
     * This will need to be revised during production
     */
    AlertDialog.Builder decisionTime = new AlertDialog.Builder(getApplicationContext());

    /**
     * Handles advancing non character based conversations at the beginning
     * of the story.
     * If the player decides to investigate the scream, then the app loads the appropriate activity, and the same with the
     * decision to leave the scene.
     */
    public void advanceGame(View view){

        clickCount = clickCount + 1; // Increments clickCount by 1 per click

        if (clickCount == 1){
            storyText.setText(R.string.story_opening_two);
        } else if(clickCount == 2){
            decisionTime.setTitle("Decision Time!"); // Sets the title for the decision box
            decisionTime.setCancelable(false); // Sets it so that the decision can not be cancelled.
            decisionTime.setPositiveButton("Investigate",new DialogInterface.OnClickListener() { // Sets up the positive button
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            decisionTime.setNegativeButton("Leave the scene.",new DialogInterface.OnClickListener() { // Sets up the negative button
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
        }
    }

    /**
     * Android activity methods
     * @param savedInstanceState is the apps savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_start);
    }
}

GameStart Layout XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_school_gate"
    android:orientation="vertical">

    <fragment
        android:id="@+id/controlsFragment"
        android:name="com.saphiric.simproject.ControlsFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/controls_fragment" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

TextView storyText = (TextView) findViewById(R.id.storyText);

只有在设置内容视图后才能调用此方法。 最终结果应该是:

TextView storyText;

然后on onCreate

setContentView(R.layout.activity_game_start);
storyText = (TextView) findViewById(R.id.storyText);

答案 1 :(得分:0)

不,你在这里没有任何片段,请在How to use fragments上阅读更多内容。

但那不是你的问题它应该按照你的方式运作。 唯一的问题是您在构造函数中调用了findViewById,甚至在此之前。

findViewById仅在您拥有用户界面时才有效,因此您必须在setContentView onCreate之后进行所有用户界面初始化!

TextView storyText;
...
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_start);
    storyText = (TextView) findViewById(R.id.storyText);
}

也不要忘记在show()上致电decisionTime

答案 2 :(得分:0)

1)发生这种情况的原因是,在您尝试获取该视图时,该视图尚未实例化。

2)如何修复它:如果你有Fragment,那么你应该在View方法中引用它Fragment.onCreateView()。然后,您可以设置ActivityFragment对话,对View进行必要的更改。