使用EclipseJava的Android应用程序出现错误

使用EclipseJava的Android应用程序出现错误,android,Android,我有一个活动,并在此活动中使用Listview、ImageButton、Edittext和微调器。但我的问题是,每当我运行代码时,它都会显示错误 08-11 06:38:38.433: D/dalvikvm(1012): GC_FOR_ALLOC freed 133K, 7% free 2745K/2940K, paused 172ms, total 206ms 08-11 06:38:38.713: E/TCP Client(1012): C: Connecting... 08-11 06:3

我有一个活动,并在此活动中使用Listview、ImageButton、Edittext和微调器。但我的问题是,每当我运行代码时,它都会显示错误

08-11 06:38:38.433: D/dalvikvm(1012): GC_FOR_ALLOC freed 133K, 7% free 2745K/2940K, paused 172ms, total 206ms
08-11 06:38:38.713: E/TCP Client(1012): C: Connecting...
08-11 06:38:38.723: D/AndroidRuntime(1012): Shutting down VM
08-11 06:38:38.723: W/dalvikvm(1012): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-11 06:38:38.973: E/AndroidRuntime(1012): FATAL EXCEPTION: main
08-11 06:38:38.973: E/AndroidRuntime(1012): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android_examples.com.tabhost/com.android_examples.com.tabhost.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android_examples.com.tabhost/com.android_examples.com.tabhost.TabActivity_1}: java.lang.NullPointerException
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.os.Looper.loop(Looper.java:137)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at java.lang.reflect.Method.invokeNative(Native Method)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at java.lang.reflect.Method.invoke(Method.java:525)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at dalvik.system.NativeStart.main(Native Method)
08-11 06:38:38.973: E/AndroidRuntime(1012): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android_examples.com.tabhost/com.android_examples.com.tabhost.TabActivity_1}: java.lang.NullPointerException
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2054)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.widget.TabHost.setCurrentTab(TabHost.java:413)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.widget.TabHost.addTab(TabHost.java:240)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at com.android_examples.com.tabhost.MainActivity.onCreate(MainActivity.java:44)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.Activity.performCreate(Activity.java:5133)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-11 06:38:38.973: E/AndroidRuntime(1012):     ... 11 more
08-11 06:38:38.973: E/AndroidRuntime(1012): Caused by: java.lang.NullPointerException
08-11 06:38:38.973: E/AndroidRuntime(1012):     at com.android_examples.com.tabhost.TabActivity_1.onCreate(TabActivity_1.java:50)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.Activity.performCreate(Activity.java:5133)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-11 06:38:38.973: E/AndroidRuntime(1012):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-11 06:38:38.973: E/AndroidRuntime(1012):     ... 21 more
08-11 06:38:46.273: I/Process(1012): Sending signal. PID: 1012 SIG: 9
我的活动代码是

public  class TabActivity_1 extends Activity {
private ListView mList;
private ArrayList<String> arrayList;
private MyCustomAdapter mAdapter;
private TCPClient mTcpClient;
Spinner sp1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_activity_1);

    sp1=(Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> ar=ArrayAdapter.createFromResource(this,R.array.Type1,android.R.layout.simple_list_item_1);
    ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    sp1.setAdapter(ar);

     arrayList = new ArrayList<String>();

     final EditText editText = (EditText) findViewById(R.id.editText1);
        ImageButton startt = (ImageButton)findViewById(R.id.imageButton1);

        //relate the listView from java to the one created in xml
        mList = (ListView)findViewById(R.id.listView1);
        mAdapter = new MyCustomAdapter(this, arrayList);
        mList.setAdapter(mAdapter);

        // connect to the server
        new connectTask().execute("");

       startt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                String message = editText.getText().toString();

                //add the text in the arrayList
                arrayList.add("c: " + message);

                //refresh the list
                mAdapter.notifyDataSetChanged();
                editText.setText("");
            }
        });
}

        public class connectTask extends AsyncTask<String,String,TCPClient> {

            @Override
            protected TCPClient doInBackground(String... message) {

                //we create a TCPClient object and
                mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
                    @Override
                    //here the messageReceived method is implemented
                    public void messageReceived(String message) {
                        //this method calls the onProgressUpdate
                        publishProgress(message);
                    }
                });
                mTcpClient.run();

                return null;
            }

            @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);

                //in the arrayList we add the messaged received from server
                arrayList.add(values[0]);
                // notify the adapter that the data set has changed. This means that new message received
                // from server was added to the list
                mAdapter.notifyDataSetChanged();
            }

        }            

         public void buttonClickMethod(View v){
              Intent i= new Intent(TabActivity_1.this,PageAjout.class);
              startActivity(i);
          }
        }
公共类选项卡活动\u 1扩展活动{
私有ListView-mList;
私有ArrayList ArrayList;
私人MyCustomAdapter mAdapter;
私有TCP客户端mTcpClient;
Spinner sp1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_选项卡_activity_1);
sp1=(微调器)findViewById(R.id.spinner1);
ArrayAdapter ar=ArrayAdapter.createFromResource(这是R.array.Type1,android.R.layout.simple\u list\u item\u 1);
ar.setDropDownViewResource(android.R.layout.simple\u dropdown\u item\u 1line);
设置适配器(ar);
arrayList=新的arrayList();
final EditText EditText=(EditText)findViewById(R.id.editText1);
ImageButton startt=(ImageButton)findViewById(R.id.imageButton1);
//将java中的listView与xml中创建的listView关联起来
mList=(ListView)findViewById(R.id.listView1);
mAdapter=新的MyCustomAdapter(此为arrayList);
mList.setAdapter(mAdapter);
//连接到服务器
新建connectTask()。执行(“”);
startt.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
String message=editText.getText().toString();
//在arrayList中添加文本
添加(“c:+消息”);
//刷新列表
mAdapter.notifyDataSetChanged();
editText.setText(“”);
}
});
}
公共类connectTask扩展了AsyncTask{
@凌驾
受保护的TCPClient doInBackground(字符串…消息){
//我们创建一个TCPClient对象并
mTcpClient=新的TCPClient(新的TCPClient.OnMessageReceived(){
@凌驾
//这里实现了messageReceived方法
已接收公共无效消息(字符串消息){
//此方法调用onProgressUpdate
出版进度(信息);
}
});
mTcpClient.run();
返回null;
}
@凌驾
受保护的void onProgressUpdate(字符串…值){
super.onProgressUpdate(值);
//在arrayList中,我们添加从服务器接收的消息
arrayList.add(值[0]);
//通知适配器数据集已更改。这意味着收到新消息
//从服务器添加到列表中
mAdapter.notifyDataSetChanged();
}
}            
公共无效按钮单击方法(视图五){
意图i=新意图(TabActivity_1.this,PageAjout.class);
星触觉(i);
}
}

我很困惑,我哪里做错了。指导我。

您将空的arrayList设置为adapter,这就是代码给出NullPointerException的原因

onCreate()

并重写
AsyncTask的
onPostExecute()
方法,并将arrayList设置为adapter。您还可以设置一个条件来检查arrayList是否不应为null

@Override
protected void onProgressUpdate(String... values) {
      super.onProgressUpdate(values);

       //in the arrayList we add the messaged received from server
      arrayList.add(values[0]);
}
protected void onPostExecute (TCPClient result) {
     mAdapter = new MyCustomAdapter(this, arrayList);
     mList.setAdapter(mAdapter);
}

希望有帮助。

具体哪一行?请不要将错误作为图片发布。另外,请更详细地描述该问题。您确定已正确初始化编辑文本吗?请输入tabActivity_1代码,因为此错误发生在您忘记任何初始化时,如edirText=(EditText)findViewById(R.id.EditText);或者为startt-alsodebug-debug-debug检查相同的代码。。。。。。请在此更新您的日志和代码
@Override
protected void onProgressUpdate(String... values) {
      super.onProgressUpdate(values);

       //in the arrayList we add the messaged received from server
      arrayList.add(values[0]);
}
protected void onPostExecute (TCPClient result) {
     mAdapter = new MyCustomAdapter(this, arrayList);
     mList.setAdapter(mAdapter);
}