2

I have this code and I dont know what is the error: package com.shoukurasou;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;
import android.widget.*;

public class ShoutActivity extends Activity {

    public void update_shouts() throws IOException{

            URL url;
            String inputLine = null;
            url = new URL("http://MYSITE/shouts.php");
            URLConnection conn = url.openConnection();
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "Windows-1252"));
            inputLine = br.readLine().toString();

            List<String> shouts = new ArrayList<String>(); 

            String[] shoutsplit = inputLine.split("<item>");

            for(int i = 0; i < shoutsplit.length; i++){

                String username = shoutsplit[i].split("<info>")[0].toString();
                String shout = shoutsplit[i].split("<info>")[1].toString();

                shouts.add(username+": "+Html.fromHtml(shout));
            }

            final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, shouts);
            final ListView list = (ListView) findViewById(R.id.list);
            if(list != null){

                ShoutActivity.this.runOnUiThread(new Runnable() {

                    public void run() {
                        list.setAdapter(null);
                        list.setAdapter(adapter);
                    }
                });

            }else{
                new AlertDialog.Builder(this).setTitle("Erro!").setMessage("Este erro é comum, por favor, feche e abra o aplicativo novamente.").setNeutralButton("Ok", null).show();
            }

            br.close();            
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shout);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                try {
                    update_shouts();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }, 0, 1000);

    }    

    public void sendShout(View v) throws IOException{

        String login = null;
        String password = null;
        String edit1value = null;

        Bundle extras = getIntent().getExtras();
        login = extras.getString("login").toString();
        password = extras.getString("password").toString();

        EditText edit1 = (EditText) findViewById(R.id.editText1);
        edit1value =  URLEncoder.encode(edit1.getText().toString());

        URL shout;
        shout = new URL("http://MYSITE/send_shout.php?username="+login+"&password="+password+"&shout="+edit1value);
        URLConnection connection_shout = shout.openConnection();
        BufferedReader brshout = new BufferedReader(new InputStreamReader(connection_shout.getInputStream()));

        edit1.setText("");

    }

}

Error:

11-11 01:34:34.649: E/AndroidRuntime(272): FATAL EXCEPTION: Timer-0 11-11 01:34:34.649: E/AndroidRuntime(272): java.lang.ArrayIndexOutOfBoundsException 11-11 01:34:34.649: E/AndroidRuntime(272): at com.shoukurasou.ShoutActivity.update_shouts(ShoutActivity.java:40) 11-11 01:34:34.649: E/AndroidRuntime(272): at com.shoukurasou.ShoutActivity$2.run(ShoutActivity.java:76) 11-11 01:34:34.649: E/AndroidRuntime(272): at java.util.Timer$TimerImpl.run(Timer.java:289)

I dont know what is the error, and I'm sorry about my english, I'm from brazil, thx

4

2 回答 2

0

此行需要在 UI 线程上调用,因为getView()从适配器管理 UI

尝试以这种方式运行它:

ShoutActivity.this.runOnUiThread(new Runnable() {

    public void run() {
        list.setAdapter(adapter);
    }
});

list并且adapter必须被标记final

于 2013-11-10T20:08:43.410 回答
0

为了在线程中修改 ui,您必须实现一个处理程序。

首先,添加:

Looper.prepare();

到您的计时器run()方法的开头。

然后你可以像这样创建一个处理程序:

Handler handler = new Handler();

然后你应该将你的 listview 代码添加到一个可运行文件中,以便我们的处理程序像这样运行:

handler.post(new Runnable(){
@Override
public void run(){
ListView list = (ListView) findViewById(R.id.list);
            list.setAdapter(null);
            list.setAdapter(adapter)
}

那应该可以解决您的问题!

于 2013-11-10T20:09:42.197 回答