1

私はスピナーを持っている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

使用することをお勧めします

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 に答える
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 に答える