我是Android新手,我需要解决此错误。我有这个聊天应用程序,我使用Java中使用的代码创建并在android ui中实现,但似乎它不起作用。我认为它是AsyncTask的一个问题
我发布代码和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);
}}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.A_apps.test_im"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
和Logcat
09-24 05:37:54.906: D/AndroidRuntime(1610): Shutting down VM
09-24 05:37:54.906: W/dalvikvm(1610): threadid=1: thread exiting with uncaught exception (group=0xb3a7fba8)
09-24 05:37:54.916: E/AndroidRuntime(1610): FATAL EXCEPTION: main
09-24 05:37:54.916: E/AndroidRuntime(1610): Process: com.A_apps.test_im, PID: 1610
09-24 05:37:54.916: E/AndroidRuntime(1610): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.A_apps.test_im/com.A_apps.test_im.MainActivity}: java.lang.NullPointerException
09-24 05:37:54.916: E/AndroidRuntime(1610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
09-24 05:37:54.916: E/AndroidRuntime(1610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-24 05:37:54.916: E/AndroidRuntime(1610): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-24 05:37:54.916: E/AndroidRuntime(1610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-24 05:37:54.916: E/AndroidRuntime(1610): at android.os.Handler.dispatchMessage(Handler.java:102)
09-24 05:37:54.916: E/AndroidRuntime(1610): at android.os.Looper.loop(Looper.java:136)
09-24 05:37:54.916: E/AndroidRuntime(1610): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-24 05:37:54.916: E/AndroidRuntime(1610): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 05:37:54.916: E/AndroidRuntime(1610): at java.lang.reflect.Method.invoke(Method.java:515)
09-24 05:37:54.916: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-24 05:37:54.916: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-24 05:37:54.916: E/AndroidRuntime(1610): at dalvik.system.NativeStart.main(Native Method)
09-24 05:37:54.916: E/AndroidRuntime(1610): Caused by: java.lang.NullPointerException
09-24 05:37:54.916: E/AndroidRuntime(1610): at android.app.Activity.findViewById(Activity.java:1884)
09-24 05:37:54.916: E/AndroidRuntime(1610): at com.A_apps.test_im.MainActivity.<init>(MainActivity.java:45)
09-24 05:37:54.916: E/AndroidRuntime(1610): at java.lang.Class.newInstanceImpl(Native Method)
09-24 05:37:54.916: E/AndroidRuntime(1610): at java.lang.Class.newInstance(Class.java:1208)
09-24 05:37:54.916: E/AndroidRuntime(1610): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
09-24 05:37:54.916: E/AndroidRuntime(1610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
09-24 05:37:54.916: E/AndroidRuntime(1610): ... 11 more
09-24 05:37:57.406: I/Process(1610): Sending signal. PID: 1610 SIG: 9
更新
The logcat
09-24 06:27:04.213: D/dalvikvm(1218): GC_FOR_ALLOC freed 61K, 5% free 2977K/3108K, paused 44ms, total 46ms
09-24 06:27:04.223: I/dalvikvm-heap(1218): Grow heap (frag case) to 3.449MB for 500416-byte allocation
09-24 06:27:04.273: D/dalvikvm(1218): GC_FOR_ALLOC freed 4K, 4% free 3461K/3600K, paused 50ms, total 50ms
09-24 06:27:04.473: W/dalvikvm(1218): threadid=11: thread exiting with uncaught exception (group=0xb3a46ba8)
09-24 06:27:04.483: E/AndroidRuntime(1218): FATAL EXCEPTION: AsyncTask #1
09-24 06:27:04.483: E/AndroidRuntime(1218): Process: com.A_apps.test_im, PID: 1218
09-24 06:27:04.483: E/AndroidRuntime(1218): java.lang.RuntimeException: An error occured while executing doInBackground()
09-24 06:27:04.483: E/AndroidRuntime(1218): at android.os.AsyncTask$3.done(AsyncTask.java:300)
09-24 06:27:04.483: E/AndroidRuntime(1218): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
09-24 06:27:04.483: E/AndroidRuntime(1218): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
09-24 06:27:04.483: E/AndroidRuntime(1218): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
09-24 06:27:04.483: E/AndroidRuntime(1218): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-24 06:27:04.483: E/AndroidRuntime(1218): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-24 06:27:04.483: E/AndroidRuntime(1218): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-24 06:27:04.483: E/AndroidRuntime(1218): at java.lang.Thread.run(Thread.java:841)
09-24 06:27:04.483: E/AndroidRuntime(1218): Caused by: java.lang.NullPointerException
09-24 06:27:04.483: E/AndroidRuntime(1218): at com.A_apps.test_im.Task.doInBackground(Task.java:29)
09-24 06:27:04.483: E/AndroidRuntime(1218): at com.A_apps.test_im.Task.doInBackground(Task.java:1)
09-24 06:27:04.483: E/AndroidRuntime(1218): at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-24 06:27:04.483: E/AndroidRuntime(1218): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-24 06:27:04.483: E/AndroidRuntime(1218): ... 4 more
09-24 06:27:04.623: D/(1218): HostConnection::get() New Host Connection established 0xb8d4d2d0, tid 1218
09-24 06:27:04.763: W/EGL_emulation(1218): eglSurfaceAttrib not implemented
09-24 06:27:04.773: D/OpenGLRenderer(1218): Enabling debug mode 0
09-24 06:27:08.543: I/Process(1218): Sending signal. PID: 1218 SIG: 9
更新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
答案 0 :(得分:2)
你应该移动
TextView textview1 = (TextView)findViewById(R.id.textView1);
EditText mymsg = (EditText)findViewById(R.id.editText1);
在onCreate()
setContentView(R.layout.activity_main);
之后的MainActivity
下
答案 1 :(得分:0)
更改您的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]);
}
}
并替换
task = new Task();
到
task = new Task(this);
在mainactivity中初始化函数