• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps The application has stopped responding

Hi Friends i am new to android application development. I have just download adt bundle and created an android project.

I want to do some thing on the page loading(means when my application opens it should read the wifi data and display the data in the page controls like TextEdit.

Where should i put the code so that it works like a pageload event.
Plz suggest
Thank u all in advance
 
Hi frnds i am new to android application development using java

I want to receive multicast data on a android application when it loads. I want that whenever the Activity loads it should start receiving multicast data and display in the textbox. But when i run this code on any emulator or Real Android device it show an error The app has stopped responding and also it does not receives any dataBelow
is the code



[HIGH]package com.example.cdttiming;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;


public class MainActivity extends Activity
{

@Override

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText Seconds;
Seconds =(EditText)findViewById(R.id.Seconds);

WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();

InetAddress ia = null;
byte[] buffer = new byte[65535];
MulticastSocket ms = null;
int port = 4321;
try
{
ia = InetAddress.getByName("226.1.1.1");
DatagramPacket dp = new DatagramPacket(buffer, buffer.length,ia,port);
ms = new MulticastSocket(port);
ms.setReuseAddress(true);
ms.joinGroup(ia);


// while (true)
for(int i=0;i<10;i++)
{
ms.receive(dp);
String s = new String(dp.getData(),0,dp.getLength());
Seconds.setText(s);
}



}
catch (UnknownHostException e)
{
Seconds.setText(e.getMessage());

}
catch (IOException e)
{
Seconds.setText(e.getMessage());
}



}
[/HIGH]

Logcat output


[HIGH]
07-09 14:26:53.116: E/AndroidRuntime(383): FATAL EXCEPTION: main
07-09 14:26:53.116: E/AndroidRuntime(383): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cdttiming/com.example.cdttiming.MainActivity}: android.os.NetworkOnMainThreadException
07-09 14:26:53.116: E/AndroidRuntime(383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
07-09 14:26:53.116: E/AndroidRuntime(383): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
07-09 14:26:53.116: E/AndroidRuntime(383): at android.app.ActivityThread.access$500(ActivityThread.java:122)
07-09 14:26:53.116: E/AndroidRuntime(383): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
07-09 14:26:53.116: E/AndroidRuntime(383): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 14:26:53.116: E/AndroidRuntime(383): at android.os.Looper.loop(Looper.java:132)
07-09 14:26:53.116: E/AndroidRuntime(383): at android.app.ActivityThread.main(ActivityThread.java:4123)
07-09 14:26:53.116: E/AndroidRuntime(383): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 14:26:53.116: E/AndroidRuntime(383): at java.lang.reflect.Method.invoke(Method.java:491)
07-09 14:26:53.116: E/AndroidRuntime(383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-09 14:26:53.116: E/AndroidRuntime(383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-09 14:26:53.116: E/AndroidRuntime(383): at dalvik.system.NativeStart.main(Native Method)
07-09 14:26:53.116: E/AndroidRuntime(383): Caused by: android.os.NetworkOnMainThreadException
07-09 14:26:53.116: E/AndroidRuntime(383): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
07-09 14:26:53.116: E/AndroidRuntime(383): at dalvik.system.BlockGuard$WrappedNetworkSystem.recv(BlockGuard.java:332)
07-09 14:26:53.116: E/AndroidRuntime(383): at org.apache.harmony.luni.net.PlainDatagramSocketImpl.doRecv(PlainDatagramSocketImpl.java:165)
07-09 14:26:53.116: E/AndroidRuntime(383): at org.apache.harmony.luni.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:174)
07-09 14:26:53.116: E/AndroidRuntime(383): at java.net.DatagramSocket.receive(DatagramSocket.java:391)
07-09 14:26:53.116: E/AndroidRuntime(383): at com.example.cdttiming.MainActivity.onCreate(MainActivity.java:50)
07-09 14:26:53.116: E/AndroidRuntime(383): at android.app.Activity.performCreate(Activity.java:4397)
07-09 14:26:53.116: E/AndroidRuntime(383): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
07-09 14:26:53.116: E/AndroidRuntime(383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
07-09 14:26:53.116: E/AndroidRuntime(383): ... 11 more

[/HIGH]

plz tell me what is the problem. Thank u all in advance
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones