Android &引用;不幸的是,应用程序已停止;与NPE

Android &引用;不幸的是,应用程序已停止;与NPE,android,Android,我是Android新手,我需要解决这个错误。我有一个聊天应用程序,它是我用Java代码创建的,在android用户界面上实现,但它似乎不起作用。我认为这是异步任务的问题 我将代码和logcat与下面的清单一起发布 主要活动 package com.A_apps.test_im; import java.io.DataInputStream; import java.io.IOException; import java.io.PrintStream; import java.net.Socket

我是Android新手,我需要解决这个错误。我有一个聊天应用程序,它是我用Java代码创建的,在android用户界面上实现,但它似乎不起作用。我认为这是异步任务的问题

我将代码和logcat与下面的清单一起发布

主要活动

package com.A_apps.test_im;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity:
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    Task task;
    boolean closed = false;

//Thread

Thread Clientthread;

// The client socket

Socket clientSocket = null;

// The output stream

PrintStream os = null;

// The input stream

DataInputStream is = null;

TextView textview1 = (TextView)findViewById(R.id.textView1);
 EditText mymsg = (EditText)findViewById(R.id.editText1);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

     StrictMode.setThreadPolicy(policy); 
     Start();

    }


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
public void Start(){
     try {
             clientSocket = new Socket("192.168.1.10", 2222);
             os = new PrintStream(clientSocket.getOutputStream());
             is = new DataInputStream(clientSocket.getInputStream());
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
public void sendMessage(){
    String msg = null;
    while (!closed&&(msg = mymsg.getText().toString()) != null) {
        os.println(msg);
        textview1.append("\nMe: "+msg);
      }

}
public void initialize(){

    if (clientSocket != null && os != null && is != null) {
        /* Create a thread to read from the server. */
        task = new Task();
        task.execute(textview1,mymsg);
        //Clientthread =  new Thread(new Runnable(){

            /*@Override
            public void run() {
                 /*
                 * Keep on reading from the socket till we receive "Bye" from the
                 * server. Once we received that then we want to break.

                os.println("Arnav");
                String responseLine;
                try {
                  while ((responseLine = is.readLine()) != null) {
                    textview1.append("\nOther: "+responseLine);

                  }
                  closed = true;
                } catch (IOException e) {
                  System.err.println("IOException:  " + e);
                }}*/
        //  });
      //   Clientthread.start();
      }

}
public void CloseConnections(){
    try {
        os.close();
        is.close();
        clientSocket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}}
任务类

package com.A_apps.test_im;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.URL;

import android.os.AsyncTask;
import android.widget.EditText;
import android.widget.TextView;

public class Task extends AsyncTask<TextView, EditText, Socket>{
String responseLine;
MainActivity activity = new MainActivity();
 protected void onPostExecute(Long result) {

 }
@Override
protected Socket doInBackground(TextView... params) {


        try {
          while ((responseLine = activity.is.readLine()) != null) {
           runOnUiThread();
          }
          activity.closed = true;
        } catch (IOException e) {
          System.err.println("IOException:  " + e);
        }
        return null;
}


public final void runOnUiThread(){ 
    activity.textview1.append("\nOther: "+responseLine);
}}
更新2:

09-24 07:38:39.123: D/dalvikvm(1541): GC_FOR_ALLOC freed 47K, 4% free 2977K/3092K, paused 107ms, total 109ms
09-24 07:38:39.123: I/dalvikvm-heap(1541): Grow heap (frag case) to 3.449MB for 500416-byte allocation
09-24 07:38:39.203: D/dalvikvm(1541): GC_FOR_ALLOC freed 3K, 4% free 3462K/3584K, paused 72ms, total 72ms
09-24 07:38:39.463: D/(1541): HostConnection::get() New Host Connection established 0xb8d4b9d8, tid 1541
09-24 07:38:39.563: W/EGL_emulation(1541): eglSurfaceAttrib not implemented
09-24 07:38:39.573: D/OpenGLRenderer(1541): Enabling debug mode 0
09-24 07:38:56.213: D/dalvikvm(1541): GC_FOR_ALLOC freed 344K, 11% free 3632K/4044K, paused 36ms, total 37ms
09-24 07:39:00.433: D/dalvikvm(1541): GC_FOR_ALLOC freed 431K, 12% free 3713K/4212K, paused 44ms, total 45ms
09-24 07:39:03.113: D/dalvikvm(1541): GC_FOR_ALLOC freed 263K, 12% free 3753K/4248K, paused 35ms, total 35ms
09-24 07:39:05.553: D/dalvikvm(1541): GC_FOR_ALLOC freed 277K, 12% free 3823K/4316K, paused 38ms, total 38ms
09-24 07:39:09.783: D/dalvikvm(1541): GC_FOR_ALLOC freed 404K, 11% free 3879K/4352K, paused 41ms, total 41ms
09-24 07:39:13.133: D/dalvikvm(1541): GC_FOR_ALLOC freed 339K, 11% free 3942K/4420K, paused 39ms, total 39ms
09-24 07:39:13.133: I/dalvikvm-heap(1541): Grow heap (frag case) to 4.039MB for 131076-byte allocation 
09-24 07:39:13.183: D/dalvikvm(1541): GC_FOR_ALLOC freed 64K, 12% free 4006K/4552K, paused 40ms, total 40ms
09-24 07:39:18.623: D/dalvikvm(1541): GC_FOR_ALLOC freed 453K, 12% free 4050K/4572K, paused 42ms, total 43ms
09-24 07:39:18.713: D/dalvikvm(1541): GC_FOR_ALLOC freed 19K, 12% free 4066K/4608K, paused 38ms, total 38ms
09-24 07:39:18.763: D/dalvikvm(1541): GC_FOR_ALLOC freed 17K, 13% free 4083K/4644K, paused 39ms, total 39ms
09-24 07:39:18.763: I/dalvikvm-heap(1541): Grow heap (frag case) to 4.083MB for 32772-byte allocation
09-24 07:39:19.853: D/dalvikvm(1541): GC_FOR_ALLOC freed 0K, 13% free 4115K/4680K, paused 1083ms, total 1083ms
09-24 07:39:19.853: I/dalvikvm-heap(1541): Grow heap (frag case) to 4.114MB for 32772-byte allocation
09-24 07:39:19.903: D/dalvikvm(1541): GC_FOR_ALLOC freed 32K, 13% free 4115K/4716K, paused 45ms, total 45ms
09-24 07:39:25.163: D/dalvikvm(1541): GC_FOR_ALLOC freed 462K, 12% free 4164K/4716K, paused 42ms, total 42ms
09-24 07:39:28.573: D/dalvikvm(1541): GC_FOR_ALLOC freed 328K, 11% free 4199K/4716K, paused 48ms, total 48ms
09-24 07:39:28.583: I/dalvikvm-heap(1541): Grow heap (frag case) to 4.290MB for 131068-byte allocation
09-24 07:39:28.623: D/dalvikvm(1541): GC_FOR_ALLOC freed 64K, 13% free 4263K/4848K, paused 46ms, total 46ms
09-24 07:39:33.803: D/dalvikvm(1541): GC_FOR_ALLOC freed 462K, 12% free 4312K/4848K, paused 49ms, total 49ms
09-24 07:39:35.643: D/dalvikvm(1541): GC_FOR_ALLOC freed 171K, 11% free 4330K/4848K, paused 46ms, total 46ms
09-24 07:39:35.643: I/dalvikvm-heap(1541): Grow heap (frag case) to 4.542MB for 262148-byte allocation
09-24 07:39:35.703: D/dalvikvm(1541): GC_FOR_ALLOC freed 128K, 13% free 4458K/5108K, paused 46ms, total 47ms
09-24 07:39:41.923: D/dalvikvm(1541): GC_FOR_ALLOC freed 519K, 12% free 4513K/5108K, paused 49ms, total 49ms
09-24 07:39:46.733: D/dalvikvm(1541): GC_FOR_ALLOC freed 431K, 11% free 4559K/5108K, paused 53ms, total 53ms
09-24 07:39:46.933: I/dalvikvm(1541): threadid=3: reacting to signal 3
09-24 07:39:46.943: D/dalvikvm(1541): GC_FOR_ALLOC freed 37K, 12% free 4593K/5176K, paused 149ms, total 149ms
09-24 07:39:47.033: I/dalvikvm(1541): Wrote stack traces to '/data/anr/traces.txt'
09-24 07:39:47.183: D/dalvikvm(1541): GC_FOR_ALLOC freed 32K, 12% free 4625K/5244K, paused 150ms, total 150ms
你该走了

TextView textview1 = (TextView)findViewById(R.id.textView1);
EditText mymsg = (EditText)findViewById(R.id.editText1);

setContentView(R.layout.activity_main)之后的
onCreate()下
main活动中的code>更改task.java文件

package com.A_apps.test_im;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.URL;

import android.os.AsyncTask;
import android.widget.EditText;
import android.widget.TextView;

public class Task extends AsyncTask<TextView, String, Socket>{
String responseLine;
  MainActivity activity;

public Task(MainActivity activity){
   this.activity= activity;

}

protected void onPostExecute(Long result) {

}
@Override
protected Socket doInBackground(TextView... params) {


    try {
      while ((responseLine = activity.is.readLine()) != null) {
       publishProgress("\nOther: "+responseLine);
      }
      activity.closed = true;
    } catch (IOException e) {
      System.err.println("IOException:  " + e);
    }
    return null;
}

@Override
    protected void onProgressUpdate(String... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
 activity.textview1.append(values[0]);

    }

}


在mainactivity的initialize函数中

1884行的Null指针异常中,您是否可以在其中发布一行内容?您会犯很多错误,尤其是在线程方面。请阅读官方文档。同样值得一读的是:活动生命周期以及使用
new
实例化它们是否是一个好主意(提示:不是)。好的,我试过了,但每次调用initialize();如果你想要logcat,应用程序会再次停止。我会发布它,但现在,当我尝试使用sendMessage功能时,它会再次停止应用程序。请描述sendMessage和post error logI的方法。我发现问题了。没问题。应用程序运行良好,直到我点击带有
android:onclick的发送按钮:“sendMessage”
因此,当我单击“发送”按钮时,它会冻结应用程序,一段时间后,应用程序没有响应,我会发布该发送消息函数在Main Activity类中的日志
09-24 07:38:39.123: D/dalvikvm(1541): GC_FOR_ALLOC freed 47K, 4% free 2977K/3092K, paused 107ms, total 109ms
09-24 07:38:39.123: I/dalvikvm-heap(1541): Grow heap (frag case) to 3.449MB for 500416-byte allocation
09-24 07:38:39.203: D/dalvikvm(1541): GC_FOR_ALLOC freed 3K, 4% free 3462K/3584K, paused 72ms, total 72ms
09-24 07:38:39.463: D/(1541): HostConnection::get() New Host Connection established 0xb8d4b9d8, tid 1541
09-24 07:38:39.563: W/EGL_emulation(1541): eglSurfaceAttrib not implemented
09-24 07:38:39.573: D/OpenGLRenderer(1541): Enabling debug mode 0
09-24 07:38:56.213: D/dalvikvm(1541): GC_FOR_ALLOC freed 344K, 11% free 3632K/4044K, paused 36ms, total 37ms
09-24 07:39:00.433: D/dalvikvm(1541): GC_FOR_ALLOC freed 431K, 12% free 3713K/4212K, paused 44ms, total 45ms
09-24 07:39:03.113: D/dalvikvm(1541): GC_FOR_ALLOC freed 263K, 12% free 3753K/4248K, paused 35ms, total 35ms
09-24 07:39:05.553: D/dalvikvm(1541): GC_FOR_ALLOC freed 277K, 12% free 3823K/4316K, paused 38ms, total 38ms
09-24 07:39:09.783: D/dalvikvm(1541): GC_FOR_ALLOC freed 404K, 11% free 3879K/4352K, paused 41ms, total 41ms
09-24 07:39:13.133: D/dalvikvm(1541): GC_FOR_ALLOC freed 339K, 11% free 3942K/4420K, paused 39ms, total 39ms
09-24 07:39:13.133: I/dalvikvm-heap(1541): Grow heap (frag case) to 4.039MB for 131076-byte allocation 
09-24 07:39:13.183: D/dalvikvm(1541): GC_FOR_ALLOC freed 64K, 12% free 4006K/4552K, paused 40ms, total 40ms
09-24 07:39:18.623: D/dalvikvm(1541): GC_FOR_ALLOC freed 453K, 12% free 4050K/4572K, paused 42ms, total 43ms
09-24 07:39:18.713: D/dalvikvm(1541): GC_FOR_ALLOC freed 19K, 12% free 4066K/4608K, paused 38ms, total 38ms
09-24 07:39:18.763: D/dalvikvm(1541): GC_FOR_ALLOC freed 17K, 13% free 4083K/4644K, paused 39ms, total 39ms
09-24 07:39:18.763: I/dalvikvm-heap(1541): Grow heap (frag case) to 4.083MB for 32772-byte allocation
09-24 07:39:19.853: D/dalvikvm(1541): GC_FOR_ALLOC freed 0K, 13% free 4115K/4680K, paused 1083ms, total 1083ms
09-24 07:39:19.853: I/dalvikvm-heap(1541): Grow heap (frag case) to 4.114MB for 32772-byte allocation
09-24 07:39:19.903: D/dalvikvm(1541): GC_FOR_ALLOC freed 32K, 13% free 4115K/4716K, paused 45ms, total 45ms
09-24 07:39:25.163: D/dalvikvm(1541): GC_FOR_ALLOC freed 462K, 12% free 4164K/4716K, paused 42ms, total 42ms
09-24 07:39:28.573: D/dalvikvm(1541): GC_FOR_ALLOC freed 328K, 11% free 4199K/4716K, paused 48ms, total 48ms
09-24 07:39:28.583: I/dalvikvm-heap(1541): Grow heap (frag case) to 4.290MB for 131068-byte allocation
09-24 07:39:28.623: D/dalvikvm(1541): GC_FOR_ALLOC freed 64K, 13% free 4263K/4848K, paused 46ms, total 46ms
09-24 07:39:33.803: D/dalvikvm(1541): GC_FOR_ALLOC freed 462K, 12% free 4312K/4848K, paused 49ms, total 49ms
09-24 07:39:35.643: D/dalvikvm(1541): GC_FOR_ALLOC freed 171K, 11% free 4330K/4848K, paused 46ms, total 46ms
09-24 07:39:35.643: I/dalvikvm-heap(1541): Grow heap (frag case) to 4.542MB for 262148-byte allocation
09-24 07:39:35.703: D/dalvikvm(1541): GC_FOR_ALLOC freed 128K, 13% free 4458K/5108K, paused 46ms, total 47ms
09-24 07:39:41.923: D/dalvikvm(1541): GC_FOR_ALLOC freed 519K, 12% free 4513K/5108K, paused 49ms, total 49ms
09-24 07:39:46.733: D/dalvikvm(1541): GC_FOR_ALLOC freed 431K, 11% free 4559K/5108K, paused 53ms, total 53ms
09-24 07:39:46.933: I/dalvikvm(1541): threadid=3: reacting to signal 3
09-24 07:39:46.943: D/dalvikvm(1541): GC_FOR_ALLOC freed 37K, 12% free 4593K/5176K, paused 149ms, total 149ms
09-24 07:39:47.033: I/dalvikvm(1541): Wrote stack traces to '/data/anr/traces.txt'
09-24 07:39:47.183: D/dalvikvm(1541): GC_FOR_ALLOC freed 32K, 12% free 4625K/5244K, paused 150ms, total 150ms
TextView textview1 = (TextView)findViewById(R.id.textView1);
EditText mymsg = (EditText)findViewById(R.id.editText1);
package com.A_apps.test_im;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.URL;

import android.os.AsyncTask;
import android.widget.EditText;
import android.widget.TextView;

public class Task extends AsyncTask<TextView, String, Socket>{
String responseLine;
  MainActivity activity;

public Task(MainActivity activity){
   this.activity= activity;

}

protected void onPostExecute(Long result) {

}
@Override
protected Socket doInBackground(TextView... params) {


    try {
      while ((responseLine = activity.is.readLine()) != null) {
       publishProgress("\nOther: "+responseLine);
      }
      activity.closed = true;
    } catch (IOException e) {
      System.err.println("IOException:  " + e);
    }
    return null;
}

@Override
    protected void onProgressUpdate(String... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
 activity.textview1.append(values[0]);

    }

}
task = new Task();
task = new Task(this);