简体   繁体   English

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

[英]Android application unable to run on emulator

I'm quite new to android development, I'm trying to develop a server-client application where the server is a simple java application that reads some text from a file and sends it using output stream. 我对android开发非常陌生,我正在尝试开发一个服务器-客户端应用程序,其中服务器是一个简单的Java应用程序,它从文件中读取一些文本并使用输出流将其发送。 the client is an android application that reads this stream when clicking a button and displays it on a text view. 客户端是一个android应用程序,当单击按钮时会读取此流并将其显示在文本视图中。

I'm using eclipse with ADK, and testing on an emulator here's how both codes look like: 我将eclipse与ADK一起使用,并在模拟器上进行测试,这是两种代码的样子:

Server: 服务器:

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();

          }    
    }

    }
}

Client: 客户:

-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();

        }

}

-Connecting class -连接类

    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) {}
 }
}

I know I know, there are some parts of the code that are not used .. my next task is to have this application send commands to the server ... so I'm still experimenting. 我知道我知道,代码中有一些未使用的部分..我的下一个任务是让该应用程序将命令发送到服务器...所以我仍在尝试。

when running the application on the emulator it stops before even displaying any of the GUI components. 在模拟器上运行应用程序时,它甚至在显示任何GUI组件之前都已停止。 Any idea why ? 知道为什么吗? Here's what the log file says 这是日志文件的内容

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

thanks! 谢谢!

Try moving the contents of your entire onResume() to an onCreate() method. 尝试将整个onResume()的内容移至onCreate()方法。 Always do the UI setup in onCreate(). 始终在onCreate()中进行UI设置。 Read this: Activity Lifecycle Management . 阅读此: 活动生命周期管理

And take any further queries to stackoverflow :) 并进一步查询stackoverflow :)

Cheers! 干杯!

You are getting a NullPointerException on line 20 in MainActivity, check that line. 您在MainActivity的第20行上收到NullPointerException,请检查该行。
I think this question is better suited for stackoverflow 我认为这个问题更适合Stackoverflow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM