【发布时间】:2016-06-22 16:45:27
【问题描述】:
我正在尝试从多维数组中填充微调器。但是,当我现在运行该应用程序时,出现此错误:
Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
我想我不确定如何将数组第一列中的数据放入微调器中。
//This method loads test scores into an array and populates spinner
public void nameArrayListMethod (){
InputStreamReader InputSR = null;
BufferedReader BufferedRdr = null;
String thisLine = null;
AssetManager am = getAssets();
try {
InputSR = new InputStreamReader(am.open("scoresdata/test_scores.txt"));
BufferedRdr = new BufferedReader(InputSR);
// open input stream test_scores for reading purpose.
int i = 0;
while ((thisLine = BufferedRdr.readLine()) != null) {
System.out.println(thisLine);
String[] parts = thisLine.split(" ");
testScoreList[i][0] = parts[0];
testScoreList[i][1] = parts[1];
i = i +1;
}
BufferedRdr.close();
InputSR.close();
} catch (Exception e) {
e.printStackTrace();
}
spinner= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, testScoreList[0]);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
}
这里是activity_main.xml:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Math Grade"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="English Grade"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:layout_weight="1"
android:gravity="center"
android:hint="Name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_weight="1"
android:gravity="center"
android:hint="Grade"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_weight="1"
android:gravity="center"
android:hint="Grade"/>
</LinearLayout>
编辑 2:
这是文本文件:
Clarkson 43.2 75.6
Hammond 55.2 77.3
May 82.5 80.9
现在它似乎部分工作,但它正在用第一行而不是第一列填充微调器。如何使用该列填充它?
谢谢,
空气修复
【问题讨论】:
-
请分享您的活动布局文件
-
0x0nosugar 谢谢,这个提示向我展示了我在没有关联布局的情况下调用微调器的愚蠢错误。但是我已经用实际的错误消息更新了我的帖子。
-
这是一个 NullPointerException (...) 好的,哪一行?你有没有通过“System.out.println(thisLine);”打印任何东西?
-
是的,它打印出文本文件行。不知道你说的哪条线是什么意思?这是一个致命的例外:主要。我假设参考我的新 ArrayAdapter 行。我敢打赌它不喜欢 testScoreList[0]
-
当我逐行运行代码时,它不会崩溃,直到我设置微调器的最后一行代码。它必须与我正在使用的多维数组有关。我很确定数组已填充,我已经在其他代码中测试过了。
标签: java android multidimensional-array android-spinner