1

我正在开发一个有 Spinner 的 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

}

}

我应该如何解决这个问题?

感谢您的帮助

4

2 回答 2

1

由于您正在调用textUrl = new URL(textSource); oncreate() 方法中的 textSource 将始终为“”。更好地创建一个方法并基于 onItemSelected 传递新的 textSource 值。

供您参考的示例:默认情况下,将通过 Link1

@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
        long arg3) {
    String Text = parent.getSelectedItem().toString();
    if (Text.equals("list 1")) {
        Method("path/to/textfile 1");

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

    } else {

    }
    // TODO Auto-generated method stub
}

方法 :

public void Method(String textSource) {

    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());
    }
}
于 2013-08-17T16:57:26.153 回答
1

我建议你使用

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

 }
于 2013-08-17T17:39:15.007 回答