我一直在为我的同事制作一个销售应用程序。我一直在做一个步骤,通过制作一个服务器应用程序,在我的计算机上读取一个txt文件,然后将其发送到Android手机上的应用程序,显示结果。问题是,一旦asyncTask完成,手机上的应用程序崩溃,服务器和手机应用程序都会终止。我查看了logcat,它似乎是一个nullpointer异常。我发现这很奇怪,因为我的代码中的while循环应该只允许非空数据通过。 这是我的代码:
package com.AWSbeacon;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.AWSbeacon.*;
public class MainActivity extends Activity{
String[] Names = null;
ArrayList<String> names = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button starter = (Button) this.findViewById(R.id.button1);
starter.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
new GetNames().execute();
Names = names.toArray(Names); //the error pops up here
for(int i=0; i<Names.length; i++){
TextView txtView1 = (TextView)findViewById((int) R.id.text1);
txtView1.setText((CharSequence)txtView1 + Names[i]);
}
}
});
}
class GetNames extends AsyncTask<Void,Void,Void>{
ArrayList<String> names = new ArrayList<String>();
String[] Names = null;
DataOutputStream out = null;
DataInputStream in = null;
Socket sock = null;
String T = null;
@Override
protected Void doInBackground(Void... params) {
try {
sock = new Socket("elvishknight1.noip.me", 8000);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in = new DataInputStream(sock.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out = new DataOutputStream(sock.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while(true){
out.writeUTF("ok");
T = in.readUTF();
if(in!=null && !in.readUTF().contains("end")){
names.add(T);
}
else if(in.readUTF().contains("end")){
break;
}
}
out.flush();
out.close();
in.close();
sock.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
我的服务器代码(以防万一):
package beaconserver;
import java.io.*;
import java.net.*;
import java.util.*;
public class Bserver {
static BufferedReader text = null;
static ServerSocket Sock = null;
static Socket server = null;
public static void main(String[] args) {
ArrayList<String> Names = new ArrayList<String>();
try {
text = new BufferedReader(new FileReader("/home/kyle/Documents/names.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String names = text.readLine();
while((names=text.readLine())!=null){
Names.add(names);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] Text = new String[Names.size()];
Text = Names.toArray(Text);
SendMessage(Text);
}
public static void SendMessage(String[] lines){
DataInputStream in = null;
DataOutputStream out = null;
String ok = null;
try {
Sock = new ServerSocket(8000);
} catch (IOException e) {
e.printStackTrace();
}
try{
server = Sock.accept();
in = new DataInputStream(server.getInputStream());
out = new DataOutputStream(server.getOutputStream());
for(int i=0; i<lines.length; i++){
while(ok==null){
ok = in.readUTF();
if(ok!=null){
break;
}
}
out.writeUTF(lines[i]);
}
out.writeUTF("end");
in.close();
out.flush();
out.close();
server.close();
Sock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以及logcat:
05-25 09:23:37.218: E/AndroidRuntime(9722): FATAL EXCEPTION: main
05-25 09:23:37.218: E/AndroidRuntime(9722): java.lang.NullPointerException
05-25 09:23:37.218: E/AndroidRuntime(9722): at java.util.ArrayList.toArray(ArrayList.java:514)
05-25 09:23:37.218: E/AndroidRuntime(9722): at com.AWSbeacon.MainActivity$1.onClick(MainActivity.java:37)
05-25 09:23:37.218: E/AndroidRuntime(9722): at android.view.View.performClick(View.java:4202)
05-25 09:23:37.218: E/AndroidRuntime(9722): at android.view.View$PerformClick.run(View.java:17340)
05-25 09:23:37.218: E/AndroidRuntime(9722): at android.os.Handler.handleCallback(Handler.java:725)
05-25 09:23:37.218: E/AndroidRuntime(9722): at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 09:23:37.218: E/AndroidRuntime(9722): at android.os.Looper.loop(Looper.java:137)
05-25 09:23:37.218: E/AndroidRuntime(9722): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-25 09:23:37.218: E/AndroidRuntime(9722): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 09:23:37.218: E/AndroidRuntime(9722): at java.lang.reflect.Method.invoke(Method.java:511)
05-25 09:23:37.218: E/AndroidRuntime(9722): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-25 09:23:37.218: E/AndroidRuntime(9722): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-25 09:23:37.218: E/AndroidRuntime(9722): at dalvik.system.NativeStart.main(Native Method)
提前感谢您的帮助
答案 0 :(得分:0)
您在tour onClickListener中使用了错误的名称变量。
也许您应该考虑重命名一些变量,例如使用nameArray