Android应用程序无法在模拟器上运行

时间:2013-03-17 06:22:46

标签: java emulation

我对android开发很陌生,我正在尝试开发一个服务器客户端应用程序,其中服务器是一个简单的Java应用程序,它从文件中读取一些文本并使用输出流发送它。客户端是一个Android应用程序,在单击按钮时读取此流并将其显示在文本视图上。

我在ADK中使用eclipse,并在模拟器上测试两个代码的样子:

服务器:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


public class Server {
     /**
     * @param args
     * @throws IOException 
     * @throws UnknownHostException 
     */
    public static void main(String[] args) throws UnknownHostException, IOException 
    {

        System.out.println("***********Starting ***********");
        ServerSocket servsock = new ServerSocket(12344);
        System.out.println("Waiting...");

        Socket sock = servsock.accept();
        while (true) 
        {
          System.out.println("Accepted connection : " + sock);
          File myFile = new File ("source.txt");
          while (true){
          byte [] mybytearray  = new byte [(int)myFile.length()];
          FileInputStream fis = new FileInputStream(myFile);
          BufferedInputStream bis = new BufferedInputStream(fis);
          bis.read(mybytearray,0,mybytearray.length);
           OutputStream os = sock.getOutputStream();
          System.out.println("Sending...");
          os.write(mybytearray,0,mybytearray.length);
          os.flush();

          }    
    }

    }
}

客户:

-MainActivity

package com.example.streamerclient;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity implements OnClickListener {
         Button ConnectBtn ;

          @Override
          public void onResume() {
            super.onResume();
            //setContentView(R.layout.activity_main);

            System.out.println(" on resume ");
            ConnectBtn = (Button)findViewById(R.id.ConnectButton);

            ConnectBtn.setOnClickListener(this);

            }
          @Override
          public void onPause() {
            super.onPause(); 
          }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Connecting c = new Connecting();

        }

}

- 连接班级

    package com.example.streamerclient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

import android.app.Activity;
import android.widget.TextView;

public class Connecting extends Activity implements Runnable 
{
    private Socket sock;
    private BufferedReader r;
    private BufferedWriter out;
    TextView Data;
    public Connecting ()
    {
        Data = (TextView) findViewById(R.id.DataTextView);
        Thread th = new Thread(this);
        th.start();

    }
    @Override
    public void run() {
        try
        {

        System.out.println("trying to initiated ");
        Data.setText("trying to initiated ");
        sock = new Socket("10.0.2.2",12344);
        System.out.println(" socket initiated ");
        Data.setText(" socket initiated ");
        r = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        Data.setText("  buffer reader initiated  ");
        System.out.println(" buffer reader initiated ");

        out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
        Data.setText("   buffer writer initiated  ");
        System.out.println(" buffer writer initiated ");
        final String data = r.readLine(); 
        Data.setText("   Data read is: \n"+data);
        System.out.println(" Data read is: \n"+data);        
        }
        catch (IOException ioe) { }
    }
 public void OnPause()
 {
     System.out.println(" paused");
        try {
          if (sock != null) {
            sock.getOutputStream().close();
            sock.getInputStream().close();
            sock.close();
            System.out.println(" everything is closed ");
          }
        } catch (IOException e) {}
 }
}

我知道我知道,代码的某些部分没有被使用..我的下一个任务是让这个应用程序向服务器发送命令......所以我还在试验。

在模拟器上运行应用程序时,它甚至会在显示任何GUI组件之前停止。知道为什么吗?这是日志文件的内容

03-17 08:16:30.886: W/Trace(846): Unexpected value from nativeGetEnabledTags: 0
03-17 08:16:30.886: W/Trace(846): Unexpected value from nativeGetEnabledTags: 0
03-17 08:16:30.886: W/Trace(846): Unexpected value from nativeGetEnabledTags: 0
03-17 08:16:31.016: W/Trace(846): Unexpected value from nativeGetEnabledTags: 0
03-17 08:16:31.016: W/Trace(846): Unexpected value from nativeGetEnabledTags: 0
03-17 08:16:31.126: I/System.out(846):  on resume 
03-17 08:16:31.447: D/AndroidRuntime(846): Shutting down VM
03-17 08:16:31.447: W/dalvikvm(846): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-17 08:16:31.457: E/AndroidRuntime(846): FATAL EXCEPTION: main
03-17 08:16:31.457: E/AndroidRuntime(846): java.lang.RuntimeException: Unable to resume activity {com.example.streamerclient/com.example.streamerclient.MainActivity}: java.lang.NullPointerException
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.os.Looper.loop(Looper.java:137)
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.app.ActivityThread.main(ActivityThread.java:5039)
03-17 08:16:31.457: E/AndroidRuntime(846):  at java.lang.reflect.Method.invokeNative(Native Method)
03-17 08:16:31.457: E/AndroidRuntime(846):  at java.lang.reflect.Method.invoke(Method.java:511)
03-17 08:16:31.457: E/AndroidRuntime(846):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-17 08:16:31.457: E/AndroidRuntime(846):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-17 08:16:31.457: E/AndroidRuntime(846):  at dalvik.system.NativeStart.main(Native Method)
03-17 08:16:31.457: E/AndroidRuntime(846): Caused by: java.lang.NullPointerException
03-17 08:16:31.457: E/AndroidRuntime(846):  at com.example.streamerclient.MainActivity.onResume(MainActivity.java:20)
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.app.Activity.performResume(Activity.java:5182)
03-17 08:16:31.457: E/AndroidRuntime(846):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
03-17 08:16:31.457: E/AndroidRuntime(846):  ... 12 more

谢谢!

2 个答案:

答案 0 :(得分:0)

尝试将整个onResume()的内容移动到onCreate()方法。始终在onCreate()中进行UI设置。请阅读:Activity Lifecycle Management

并进一步查询stackoverflow:)

干杯!

答案 1 :(得分:0)

您在MainActivity的第20行遇到NullPointerException,检查该行 我认为这个问题更适合stackoverflow