应用程序在setAdapter(适配器)崩溃

时间:2014-07-29 12:37:54

标签: android mysql json listview sharedpreferences

我正在开发一个Android应用程序,其中我将动态数据从mysql数据库显示到活动的ListView中。但我在list.setAdapter(适配器)上收到错误;和应用程序崩溃。在我的Activity中使用共享首选项后会发生这种情况。在活动中使用共享首选项之前,它工作正常,我在listView中获取数据。但现在我的应用程序崩溃了。我不知道究竟是因为共享偏好还是没有.. !!!请让我知道我做错了什么...... !!! 这是我的代码:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Welcome extends Activity implements OnClickListener 
{
    TextView text,t1;
    Button b1,b2,b3;
    String CompName,CompID,Uname;
    DatabaseHandler db;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        db=new DatabaseHandler(getApplicationContext());

        List<Name> name=db.getAllNames();
        Name namee=name.get(0);
        Uname=namee.getUsername();
        t1.setText("Hello "+Uname);

        text=(TextView)findViewById(R.id.textView3);
        t1=(TextView)findViewById(R.id.textView1);

        connect();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if ( keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onBackPressed() {

        return;
    }


    public void connect() 
    {
        String data;
        List<String> r = new ArrayList<String>();
        ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,r);
        ListView list=(ListView)findViewById(R.id.list);
        try 
        {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet("http://demo.example.com/tapme/Retrive.php");
                HttpResponse response = client.execute(request);
                HttpEntity entity=response.getEntity();
                data=EntityUtils.toString(entity);
                Log.e("STRING", data);

                try 
                {
                   JSONArray json=new JSONArray(data);
                   for(int i=0;i<json.length(); i++)
                   {
                        JSONObject obj=json.getJSONObject(i);

                        CompName=obj.getString("fldCompName");
                        CompID=obj.getString("fldCompID");

                        Log.e("STRING", CompName);
                        Log.e("STRING",CompID);

                        r.add(CompName);
                        //text.setText(CompName);
                        //r.add(CompID);
                        list.setAdapter(adapter);

                       list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
                        {
                            @Override
                            public void onItemClick(AdapterView<?> parent,View view, int position, long id) 
                            {
                                Intent i1=new Intent(Welcome.this,ChatActivity.class);
                                //i1.putExtra("CompName", CompName);
                                startActivity(i1);
                            }
                        });
                   }
                } 
                catch (JSONException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (ClientProtocolException e) {
                Log.d("HTTPCLIENT", e.getLocalizedMessage());
            } catch (IOException e) {
                Log.d("HTTPCLIENT", e.getLocalizedMessage());
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.welcome, menu);
        return true;
    }


    @Override
    public void onClick(View v) 
    {
        // TODO Auto-generated method stub
        if(v==b1)
        {
            Intent i1=new Intent(Welcome.this,Category1.class);
            startActivity(i1);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在初始化t1.setText("Hello "+Uname);之前设置t1。因此请更改

 t1.setText("Hello "+Uname);
 text=(TextView)findViewById(R.id.textView3);
 t1=(TextView)findViewById(R.id.textView1);

    text=(TextView)findViewById(R.id.textView3);
    t1=(TextView)findViewById(R.id.textView1);
    t1.setText("Hello "+Uname);

使用AsyncTask

进行网络操作