gpt4 book ai didi

java - 连接到 Android Spinner 时出错

转载 作者:行者123 更新时间:2023-11-29 21:36:39 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,其中有一个微调器,根据我选择的项目,该应用程序将读取文本文件编号 1 或编号 2,等等......我写了一个代码,但是当我在我的设备上运行它时,它说“应用程序意外停止”。

这是我的代码:

public class MainActivity extends Activity implements OnClickListener, OnItemSelectedListener {
Spinner spinner;
String textSource = "";
TextView textMsg;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textMsg = (TextView) findViewById(R.id.textmsg);
spinner=(Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(this);
URL textUrl;
String stringText = "";
try {
textUrl = new URL(textSource);
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(textUrl.openStream(), "ISO-8859-1"));
//ISO-8859-1
String StringBuffer;
//String stringText = "";
while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;

}

bufferReader.close();




textMsg.setText(stringText);
//textMsg.setText(string123);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
textMsg.setText(e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
textMsg.setText(e.toString());
}
}

@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
String Text = parent.getSelectedItem().toString();
if(Text.equals("list 1")) {

textSource = "path/to/textfile 1";

}
else if(Text.equals("list 2")){
textSource = "path/to/textfile 2";

}
else {

}

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

}

我该如何解决这个问题?

感谢帮助

最佳答案

我建议你使用

String Text = spinner.getSelectedItem().toString();

代替

String Text = parent.getSelectedItem().toString();

这样就变成了:

 @Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
long arg3) {

String Text = spinner.getSelectedItem().toString();

if(Text.equals("list 1")) {
textSource = "path/to/textfile 1";
}
else if(Text.equals("list 2")){
textSource = "path/to/textfile 2";
}
else {
}

// then put your URL code here as follows
URL textUrl;
String stringText = "";
try {
textUrl = new URL(textSource);
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(textUrl.openStream(), "ISO-8859-1"));
//ISO-8859-1
String StringBuffer;

while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;
}

bufferReader.close();
textMsg.setText(stringText);
//textMsg.setText(string123);
} catch (MalformedURLException e) {
e.printStackTrace();
textMsg.setText(e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
textMsg.setText(e.toString());
}

}

关于java - 连接到 Android Spinner 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18290908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com