【发布时间】:2012-04-30 13:48:25
【问题描述】:
好的,简要总结一下。该应用程序以 listView(由字符串数组填充)打开,当您点击列表中的一个项目时,它会将您带到另一个 listView(由字符串数组填充),其中包含与您点击的第一个项目相关联的项目。在第二个 listView 中,我想点击一个项目并让它显示一个带有来自另一个字符串数组的关联项目的 textView。到目前为止,我有前两个 listViews 工作,但无法让 textView 工作。我已经为此头疼了两天了,我感到非常沮丧。你能帮忙吗?!
这是我的字符串 xml 文件:
<string-array name="topics">
<item>Idioms</item>
<item>Travel</item>
<item>Small Talk</item>
<item>Tips</item>
<item>App Data</item>
</string-array>
<string-array name="idioms">
<item>Cash Chow</item>
<item>No Spring chicken</item>
</string-array>
<string-array name="travel">
<item>Asking for Change</item>
<item>Bus and Train Schedule</item>
</string-array>
<string-array name="idioms_description">
<item>A cash cow is a blah blah blah</item>
<item>No spring chicken means blah blah blah</item>
</string-array>
<string-array name="travel_description">
<item>This is a test for Asking for change</item>
<item>This is a test for Bus and train schedules</item>
</string-array>
这是我的 getIntent 和我应该 setText 的地方。我将其留空,因为到目前为止我所尝试的一切都失败了,我只是不知道如何从所选数组中获取正确的字符串。
public class DetailLanguagePoints extends Activity{
private int position;
private TextView mLanguagePoint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_view);
mLanguagePoint = (TextView) findViewById(R.detail.languagepoints);
Bundle extras = getIntent().getExtras();
//int[] arrayIds = new int[]{R.array.idioms, R.array.travel};
position = extras.getInt("listposition");
int[] stringarrayIds = new int[]{R.array.idioms_description, R.array.travel_description};
String[] subTopics = getResources().getStringArray(stringarrayIds[position]);
String description = subTopics[position];
final String TAG = "MyActivity";
Log.d(TAG,description);
mLanguagePoint.setText(description);
}
}
我发现空异常错误是什么问题,我没有在setcontentView()之后调用findViewById()。我这样做了,它解决了问题,它现在正在工作!感谢所有提供建议的人!!
【问题讨论】:
-
你有没有在 logcat 中遇到任何错误?
-
我稍微修改了代码,请在上面查看。我做了两件事,首先,我将所选 listView 项目的字符串打印到 LogCat 并打印了正确的字符串。但是当我单击 listView 中的项目时它仍然崩溃。这是我在 LogCat E/AndroidRuntime(281) 中遇到的错误:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oiapps.everydayenglish/com.oiapps.everydayenglish.DetailLanguagePoints}: java.lang.NullPointerException 知道什么可能导致它?
标签: android android-intent textview arrays settext