簡體   English   中英

設置TextView的文本時發生NullPointerException

[英]NullPointerException when setting text of 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>

有什么想法如何解決嗎? 謝謝!

這個怎么樣。 onCreate獲取您的TextViews的引用:

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

編輯:至於“ TextViews是空的”問題,您需要測試的東西,例如:

代替tv.setText(text); 嘗試使用類似tv.setText("10"); (只是一些隨機文本)。 如果將數字設置為“ 10”有效,則問題出在Integer.toString(...)部分(出於某些未知原因)。 如果將數字“ 10”設置為無效,則應嘗試在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 ,則代碼的WimmelView部分存在問題。 而且,如果仍然無法設置文本,則問題可能出在您調用initRound方法的地方,那里的某些代碼正在做一些會影響TextViews的事情。

首先在onCreate()中初始化textviews

public class MainActivity extends Activity implements View.OnClickListener {

私人TextView tv1,tv2,tv3;

私人無效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
}

}

這將解決您的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM