设置TextView文本时出现NullPointerException

时间:2015-03-07 11:10:35

标签: java android nullpointerexception textview

我正在尝试制作一款简单的游戏。当我尝试设置3 TextViews的文字时,我得到NullPointerException

public class MainActivity extends Activity implements View.OnClickListener {

private void initRound() {
    countdown = 10;

    ViewGroup container = (ViewGroup) findViewById(R.id.container);
    container.removeAllViews();
    WimmelView wv = new WimmelView(this);
    container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    wv.setImageCount(8*(10 + round));

    fillTextView(R.id.points, Integer.toString(points));
    fillTextView(R.id.round, Integer.toString(round));
    fillTextView(R.id.countdown, Integer.toString(countdown*1000));
}

private void fillTextView(int id, String text) {
    TextView tv = (TextView) findViewById(id);
    tv.setText(text);  //HERE DO I GET THE EXCEPTION
}

}

这是来自logcat的错误:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.developer.nico.kissthefrognow, PID: 1940
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.developer.nico.kissthefrognow.MainActivity.fillTextView(MainActivity.java:80)
        at com.developer.nico.kissthefrognow.MainActivity.initRound(MainActivity.java:34)
        at com.developer.nico.kissthefrognow.MainActivity.newGame(MainActivity.java:21)
        at com.developer.nico.kissthefrognow.MainActivity.startGame(MainActivity.java:67)
        at com.developer.nico.kissthefrognow.MainActivity.onClick(MainActivity.java:58)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我的xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/rl">

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/container">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="000000"
        android:id="@+id/points"
        android:layout_gravity="left|top"
        android:textColor="@color/Red" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:id="@+id/round"
        android:layout_gravity="right|top"
        android:textColor="@color/Red" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00000"
        android:id="@+id/countdown"
        android:layout_gravity="center_horizontal|bottom"
        android:textColor="@color/Red" />
</FrameLayout>

任何想法如何解决?谢谢!

2 个答案:

答案 0 :(得分:1)

这个怎么样?在onCreate中获取TextView的参考:

public class MainActivity extends Activity implements View.OnClickListener {

    TextView tvPoints;
    TextView tvRound;
    TextView tvCountdown;

    protected void onCreate(Bundle savedInstanceState) {
        ....
        ....
        tvPoints = (TextView) findViewById(R.id.points);
        tvRound = (TextView) findViewById(R.id.round);
        tvCountdown = (TextView) findViewById(R.id.countdown);
        ....
        ....
    }

然后在initRound()执行此操作:

fillTextView(tvPoints, Integer.toString(points));
fillTextView(tvRound, Integer.toString(round));
fillTextView(tvCountdown, Integer.toString(countdown*1000));

和您的fillTextView

private void fillTextView(TextView tv, String text)  {
    tv.setText(text);
}

如果这不起作用,则错误出现在某些代码行中(尝试删除它们并查看代码是否有效):

    container.removeAllViews();
    WimmelView wv = new WimmelView(this);
    container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    wv.setImageCount(8*(10 + round));

编辑:至于#34; TextViews是空的&#34;问题,你需要测试一些东西,例如:

而不是tv.setText(text);尝试使用类似tv.setText("10");的内容(只是一些随机文本)。如果设置数字&#34; 10&#34;正在运作,那么问题出在Integer.toString(...)部分(原因不明)。如果设置数字&#34; 10&#34;如果不起作用,那么您应该尝试使用initRound()方法设置文本,而不使用fillTextView方法(只是为了测试文本的设置是否正常),如下所示:

private void initRound() {
    countdown = 10;

    tvPoints.setText("10");
    tvRound.setText("7");
    tvCountdown.setText("19");    

    ViewGroup container = (ViewGroup) findViewById(R.id.container);
    container.removeAllViews();
    WimmelView wv = new WimmelView(this);
    container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    wv.setImageCount(8*(10 + round));
}

如果这样做,则代码的WimmelView部分存在一些问题。如果设置文本仍然不起作用,那么问题可能就是你调用initRound方法的地方,有些代码正在做一些影响TextViews的事情......

答案 1 :(得分:0)

首先在onCreate()中初始化文本视图

public class MainActivity extends Activity implements View.OnClickListener {

私人TextView tv1,tv2,tv3;

private void initRound(){     倒计时= 10;

ViewGroup container = (ViewGroup) findViewById(R.id.container);
container.removeAllViews();
WimmelView wv = new WimmelView(this);
container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
wv.setImageCount(8*(10 + round));
  

这是必需的 - 这样Android可以在使用它们之前找到视图

tv1 = (TextView) findViewById(R.id.points);
tv2 = (TextView) findViewById(R.id.round);
tv3 = (TextView) findViewById(R.id.countdown);


fillTextView(R.id.points, Integer.toString(points));
fillTextView(R.id.round, Integer.toString(round));
fillTextView(R.id.countdown, Integer.toString(countdown*1000));
}

private void fillTextView(int id, String text) {
    TextView tv = (TextView) findViewById(id);
    tv.setText(text);  //HERE DO I GET THE EXCEPTION
}

}

这将解决您的问题