textviewArray[i].setText(stringArray[i]); 是空的。

huangapple go评论66阅读模式
英文:

textviewArray[i].setText(stringArray[i]); is null

问题

以下是翻译好的代码部分:

public class dizilisActivity extends AppCompatActivity {
    TextView player1, player2,player3,player4,player5,player6,player7,player8,player9,player10;
    TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10};
    String[] receivedNames;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dizilis);
        player1= (TextView) findViewById(R.id.player1);
        player2= (TextView) findViewById(R.id.player2);
        player3= (TextView) findViewById(R.id.player3);
        player4= (TextView) findViewById(R.id.player4);
        player5= (TextView) findViewById(R.id.player5);
        player6 = (TextView) findViewById(R.id.player6);
        player7 = (TextView) findViewById(R.id.player7);
        player8 = (TextView) findViewById(R.id.player8);
        player9 = (TextView) findViewById(R.id.player9);
        player10 = (TextView) findViewById(R.id.player10);
        Intent intent = getIntent();
        receivedNames = intent.getStringArrayExtra("names");
        for (int i = 0; i<receivedNames.length; ++i)
            players[i].setText(receivedNames[i]);
    }
}

请注意,如果您在运行代码时遇到问题,主要错误信息是 java.lang.NullPointerException,这意味着在您的代码中,players[i] 中的某个元素为 null。要确保在使用 setText() 方法之前,每个 players[i] 都已经被正确初始化。

英文:

I created TextView Array and defined every element in array I got an a String array with intent but when I tried to set textview text with elements that received string array with for loop, it returned null
what is wrong with this codes? I am beginner in java and

public class dizilisActivity extends AppCompatActivity {
TextView player1, player2,player3,player4,player5,player6,player7,player8,player9,player10;
TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10};
String[] receivedNames;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dizilis);
    player1= (TextView) findViewById(R.id.player1);
    player2= (TextView) findViewById(R.id.player2);
    player3= (TextView) findViewById(R.id.player3);
    player4= (TextView) findViewById(R.id.player4);
    player5= (TextView) findViewById(R.id.player5);
    player6 = (TextView) findViewById(R.id.player6);
    player7 = (TextView) findViewById(R.id.player7);
    player8 = (TextView) findViewById(R.id.player8);
    player9 = (TextView) findViewById(R.id.player9);
    player10 = (TextView) findViewById(R.id.player10);
    Intent intent = getIntent();
    receivedNames = intent.getStringArrayExtra(&quot;names&quot;);
    for (int i = 0; i&lt;receivedNames.length; ++i)
    players[i].setText(receivedNames[i]);

    }

}

and logcat

> Caused by: java.lang.NullPointerException: Attempt to invoke virtual
> method 'void android.widget.TextView.setText(java.lang.CharSequence)'
> on a null object reference
> at com.easoft.aldimverdim.dizilisActivity.onCreate(dizilisActivity.java:33)

and line 33 is

players[i].setText(receivedNames[i];

答案1

得分: 2

TL;DR

  1. 将以下代码:
TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10};

改成:

TextView[] players;
  1. 将初始化移动至以下代码后:
player10 = (TextView) findViewById(R.id.player10);

添加:

players = new TextView[]{player1, player2, player3, player4, player5, player6, player7, player8, player9, player10};
  1. receivedNamesnull 时,你可以添加额外的条件,例如:
// 检查 receivedNames 不为 null
if (receivedNames != null) {
    for (int i = 0; i < receivedNames.length; ++i) {
        // 这里是其余的代码
    }
}

错误解释

  1. 代码行:
TextView player1, player2,player3,player4,player5,player6,player7,player8,player9,player10;

导致声明了 10 个变量,它们都是 null

  1. 因此,在下一行中:
TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10};

声明了一个新的数组,并使用 null 值进行初始化,所以你得到一个包含 10 个 null 的数组。

[null, null, null, null, null, null, null, null, null, null]
  1. 当在 onCreate() 中尝试调用 players[i].setText(...) 时,players[i] 具有 null 值。因此,你在 null 上调用了 setText。这是崩溃的原因。

完整可工作的代码:

public class MainActivity extends AppCompatActivity {
    TextView player1, player2, player3, player4, player5, player6, player7, player8, player9,
            player10;

    TextView[] players;

    String[] receivedNames;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dizilis);

        player1 = findViewById(R.id.player1);
        player2 = findViewById(R.id.player2);
        player3 = findViewById(R.id.player3);
        player4 = findViewById(R.id.player4);
        player5 = findViewById(R.id.player5);
        player6 = findViewById(R.id.player6);
        player7 = findViewById(R.id.player7);
        player8 = findViewById(R.id.player8);
        player9 = findViewById(R.id.player9);
        player10 = findViewById(R.id.player10);

        players = new TextView[]{player1, player2, player3, player4, player5, player6, player7,
                player8, player9, player10};

        Intent intent = getIntent();
        receivedNames = intent.getStringArrayExtra("names");

        if (receivedNames != null) {
            for (int i = 0; i < receivedNames.length; ++i) {
                players[i].setText(receivedNames[i]);
            }
        }
    }
}

提示 #1

良好的实践是从较短或“引起更多问题”的索引中获取索引,因此你可以将以下代码从:

i < receivedNames.length

改为:

i < players.length

当你从 getStringArrayExtra() 接收到较少的数据时,什么都不会发生(只是空的 TextView)。但是,当你尝试为不存在的 TextView 分配值,或者尝试为不存在的数组位置分配值时,会导致崩溃。

提示 #2

如果在循环中遇到类似的问题,你可以:

  • 使用断点(在“调试”模式下构建 Android 应用程序)
    或者
  • 添加简单的 try-catch 块来检查哪个索引引起了问题。例如:
for (int i = 0; i < receivedNames.length; ++i) {
    try {
        players[i].setText(receivedNames[i]);
    } catch (Exception e) {
        System.out.println("Problem with id = " + i);
    }
}
英文:

TL;DR

  1. Change your
    from
TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10}

in to

TextView[] players;
  1. Move initialization, so after
player10 = (TextView) findViewById(R.id.player10);

add:

players = new TextView[]{player1, player2, player3, player4, player5, player6, player7, player8, player9, player10};
  1. When receivedNames will be null, you can add extra condition, for example:
// Check if receivedNames are NOT null
if (receivedNames != null) {
for (int i = 0; i &lt; receivedNames.length; ++i) {
// here rest of codes
}
}

Error explanation

  1. Line:
TextView player1, player2,player3,player4,player5,player6,player7,player8,player9,player10;

causes that 10 variables are declared. All of them are nulls

  1. So in next line with:
TextView[] players = {player1, player2,player3,player4,player5,player6,player7,player8,player9,player10};

declare new array and initializes it with null values so you array with 10 nulls inside.

[null, null, null, null, null, null, null, null, null, null]
  1. When in onCreate() you are trying to call players[i].setText(...), the players[i] has null value. So you call setText on null. That is the crash reason.

Full working code:

public class MainActivity extends AppCompatActivity {
TextView player1, player2, player3, player4, player5, player6, player7, player8, player9,
player10;
TextView[] players;
String[] receivedNames;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dizilis);
player1 = findViewById(R.id.player1);
player2 = findViewById(R.id.player2);
player3 = findViewById(R.id.player3);
player4 = findViewById(R.id.player4);
player5 = findViewById(R.id.player5);
player6 = findViewById(R.id.player6);
player7 = findViewById(R.id.player7);
player8 = findViewById(R.id.player8);
player9 = findViewById(R.id.player9);
player10 = findViewById(R.id.player10);
players = new TextView[]{player1, player2, player3, player4, player5, player6, player7,
player8, player9, player10};
Intent intent = getIntent();
receivedNames = intent.getStringArrayExtra(&quot;names&quot;);
if (receivedNames != null) {
for (int i = 0; i &lt; receivedNames.length; ++i) {
players[i].setText(receivedNames[i]);
}
}
}
}

TIP #1

Good practice is to get index from shorter or "causing more problems", so you can change from:

i &lt; receivedNames.length

to:

i &lt; players.length

When you receive less data from getStringArrayExtra() = nothing happen (just empty TextViews).

But when you will try to assign to TextView which does not exist or to possition in array which does not exist = CRASH

TIP #2

If you will have same/similiar issue with loops you can:

  • use breakpoints (about build Android app in debug mode`)
    or
  • add simple try-catch block to check which index is causing problem. For example:
for (int i = 0; i &lt; receivedNames.length; ++i) {
try {
players[i].setText(receivedNames[i]);
} catch (Exception e) {
System.out.println(&quot;Problem with id = &quot; + i);
}
}

答案2

得分: 0

似乎你从未更新数组中的值。既然你知道数组中会有10个视图,请尝试以下操作。

private final TextView[] views = new TextView[10];

protected void onCreate(Bundle savedInstanceState) {
   views[0] = (TextView) findViewById(R.id.player1);
   views[1] = (TextView) findViewById(R.id.player2);
   // ...以此类推
}
英文:

Seems like you are never updating the values in the array. Since you know you are going to have 10 views in the array try the following.

private final TextView[] views = new TextView[10];

protected void onCreate(Bundle savedInstanceState) {
   views[0] = (TextView) findViewById(R.id.player1);
   views[1] = (TextView) findViewById(R.id.player2);
   // ... and so forth
}

答案3

得分: 0

看起来你首先通过将文本视图初始化为 null 引用来创建数组,并且之后没有更新引用,比如 players[index]=(TextView) findViewById(R.id.player1)。不要这样做,可以创建一个空数组,然后使用 for 循环动态获取文本视图 ID 并将对象推入数组中。

public class dizilisActivity extends AppCompatActivity {
    TextView[] players = new TextView[10];
    String[] receivedNames;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dizilis);

        for (int i = 0; i < 10; i++) {
            String textViewId = "player" + (i + 1);
            int resID = getResources().getIdentifier(textViewId, "id", getPackageName());
            players[i] = (TextView) findViewById(resID);
        }

        Intent intent = getIntent();
        receivedNames = intent.getStringArrayExtra("names");
        for (int i = 0; i < receivedNames.length; ++i)
            players[i].setText(receivedNames[i]);
    }
}
英文:

It seems you are first initializing the array with null references to text views and not updating the references there after like players[index]=(TextView) findViewById(R.id.player1). Instead of doing this ,create an empty array
and then use for loop for dynamically getting text view id and push object into the array.

public class dizilisActivity extends AppCompatActivity {
TextView[] players = new TextView[10];
String[] receivedNames;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dizilis);
for(int i=0;i&lt;10;i++){
String textViewId=&quot;player&quot;+(i+1);
int resID = getResources().getIdentifier(textViewId, &quot;id&quot;, getPackageName());
players[i]=(TextView) findViewById(resID);
}
Intent intent = getIntent();
receivedNames = intent.getStringArrayExtra(&quot;names&quot;);
for (int i = 0; i&lt;receivedNames.length; ++i)
players[i].setText(receivedNames[i]);
}

huangapple
  • 本文由 发表于 2020年4月6日 22:15:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61061870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定