Question

I've made a small Android application which sends a UDP packet the moment you open it.

The problem is, it crashes when you open it, instead of the sending the packet.

I mean, I've tried running it on the Eclipse's Android Virtual Device, and it crashes there.

Here's the code:

package com.example.messagesender;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;

import java.io.*;
import java.net.*;

public class Message_Sender extends Activity {
    DatagramSocket clientSocket;
    InetAddress IPAddressx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message__sender);
        try {
            clientSocket = new DatagramSocket();
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          try {
            IPAddressx = InetAddress.getByName("127.0.0.1");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
          byte[] sendData = new byte[1024];
          String sentence = "Hello World!";
          sendData = sentence.getBytes();
          DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddressx, 12345);
          try {
            clientSocket.send(sendPacket);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

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

}

Logcat Output:

01-31 19:00:58.420: W/System.err(810): java.net.SocketException: socket failed: EACCES (Permission denied)
01-31 19:00:58.420: W/System.err(810):  at libcore.io.IoBridge.socket(IoBridge.java:576)
01-31 19:00:58.420: W/System.err(810):  at java.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:91)
01-31 19:00:58.420: W/System.err(810):  at java.net.DatagramSocket.createSocket(DatagramSocket.java:133)
01-31 19:00:58.540: W/System.err(810):  at java.net.DatagramSocket.<init>(DatagramSocket.java:80)
01-31 19:00:58.540: W/System.err(810):  at java.net.DatagramSocket.<init>(DatagramSocket.java:65)
01-31 19:00:58.540: W/System.err(810):  at com.example.messagesender.Message_Sender.onCreate(Message_Sender.java:23)
01-31 19:00:58.540: W/System.err(810):  at android.app.Activity.performCreate(Activity.java:5231)
01-31 19:00:58.540: W/System.err(810):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-31 19:00:58.540: W/System.err(810):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-31 19:00:58.540: W/System.err(810):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-31 19:00:58.540: W/System.err(810):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-31 19:00:58.540: W/System.err(810):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-31 19:00:58.540: W/System.err(810):  at android.os.Handler.dispatchMessage(Handler.java:102)
01-31 19:00:58.540: W/System.err(810):  at android.os.Looper.loop(Looper.java:136)
01-31 19:00:58.590: W/System.err(810):  at android.app.ActivityThread.main(ActivityThread.java:5017)
01-31 19:00:58.590: W/System.err(810):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 19:00:58.590: W/System.err(810):  at java.lang.reflect.Method.invoke(Method.java:515)
01-31 19:00:58.590: W/System.err(810):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-31 19:00:58.590: W/System.err(810):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-31 19:00:58.590: W/System.err(810):  at dalvik.system.NativeStart.main(Native Method)
01-31 19:00:58.590: W/System.err(810): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
01-31 19:00:58.610: W/System.err(810):  at libcore.io.Posix.socket(Native Method)
01-31 19:00:58.610: W/System.err(810):  at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
01-31 19:00:58.610: W/System.err(810):  at libcore.io.IoBridge.socket(IoBridge.java:561)
01-31 19:00:58.610: W/System.err(810):  ... 19 more
01-31 19:00:58.630: D/AndroidRuntime(810): Shutting down VM
01-31 19:00:58.630: W/dalvikvm(810): threadid=1: thread exiting with uncaught exception (group=0xb1a86ba8)
01-31 19:00:58.640: E/AndroidRuntime(810): FATAL EXCEPTION: main
01-31 19:00:58.640: E/AndroidRuntime(810): Process: com.example.messagesender, PID: 810
01-31 19:00:58.640: E/AndroidRuntime(810): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.messagesender/com.example.messagesender.Message_Sender}: java.lang.NullPointerException
01-31 19:00:58.640: E/AndroidRuntime(810):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-31 19:00:58.640: E/AndroidRuntime(810):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-31 19:00:58.640: E/AndroidRuntime(810):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-31 19:00:58.640: E/AndroidRuntime(810):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-31 19:00:58.640: E/AndroidRuntime(810):  at android.os.Handler.dispatchMessage(Handler.java:102)
01-31 19:00:58.640: E/AndroidRuntime(810):  at android.os.Looper.loop(Looper.java:136)
01-31 19:00:58.640: E/AndroidRuntime(810):  at android.app.ActivityThread.main(ActivityThread.java:5017)
01-31 19:00:58.640: E/AndroidRuntime(810):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 19:00:58.640: E/AndroidRuntime(810):  at java.lang.reflect.Method.invoke(Method.java:515)
01-31 19:00:58.640: E/AndroidRuntime(810):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-31 19:00:58.640: E/AndroidRuntime(810):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-31 19:00:58.640: E/AndroidRuntime(810):  at dalvik.system.NativeStart.main(Native Method)
01-31 19:00:58.640: E/AndroidRuntime(810): Caused by: java.lang.NullPointerException
01-31 19:00:58.640: E/AndroidRuntime(810):  at com.example.messagesender.Message_Sender.onCreate(Message_Sender.java:38)
01-31 19:00:58.640: E/AndroidRuntime(810):  at android.app.Activity.performCreate(Activity.java:5231)
01-31 19:00:58.640: E/AndroidRuntime(810):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-31 19:00:58.640: E/AndroidRuntime(810):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-31 19:00:58.640: E/AndroidRuntime(810):  ... 11 more

So why is this crashing? The code looks fine to me, it's weird.

Était-ce utile?

La solution

It looks like you are trying to make network calls on the UI thread, which is a big no no.

Take a look at how to implement network operations using background threads, specifically ASyncTask There are literaly 1000's of questions and examples about this.

here is a tutorial to get you started

Also add the necessary permission to your manifest

<uses-permission android:name="android.permission.INTERNET" />

Autres conseils

You're most likely missing the INTERNET permission in your android manifest file. Add this line inside the <application> tag:

<uses-permission android:name="android.permission.INTERNET"/>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top