Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By kjkrum

Thread: The application has stopped responding

  1. #1
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default The application has stopped responding

    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

    Java Code:
    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());
    	        }
    	        
    	       
    
    	}
    
    	@Override
    	 public boolean onCreateOptionsMenu(Menu menu)
    	{
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}	
    
    }




    below is Logcat output

    Java Code:
    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

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

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: The application has stopped responding

    You get the "application not responding" error when a UI event is not processed within some fixed amount of time... five seconds, I think. This happens because you're doing too much stuff in the UI thread. And a NetworkOnMainThreadException is exactly what it sounds like: you're not allowed to do any network I/O whatsoever in the UI thread. Older (like 2.x) versions of Android allowed it with a warning; 4.x just crashes. Lines 34-66, and maybe lines 29-32 also, must be done outside the UI thread. See http://developer.android.com/referen...AsyncTask.html
    Last edited by kjkrum; 07-09-2013 at 08:23 PM.
    Get in the habit of using standard Java naming conventions!

  3. #3
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: The application has stopped responding

    Quote Originally Posted by kjkrum View Post
    You get the "application not responding" error when a UI event is not processed within some fixed amount of time... five seconds, I think. This happens because you're doing too much stuff in the UI thread. And a NetworkOnMainThreadException is exactly what it sounds like: you're not allowed to do any network I/O whatsoever in the UI thread. Older (like 2.x) versions of Android allowed it with a warning; 4.x just crashes. Lines 34-66, and maybe lines 29-32 also, must be done outside the UI thread. See AsyncTask | Android Developers


    Hi Sir

    As told by u i tried to implement the Asynchtask but it was not solving my problem .Because i had to call doInBackground(URL... urls) method every time for executing the socket.receive which was also hanging.

    i dont know exactly how to implement that. it wiill be better if u kindly format my code for AsynchTask. means how to call receive and display the things everytime.

    Thanking u in advance

    i had implemented like this
    protected void onCreate(Bundle savedInstanceState)
    {
    while(true)
    AsyncTaskRunner runner = new AsyncTaskRunner();
    runner.execute();
    }
    }

    private class AsyncTaskRunner extends AsyncTask<String, String, String> {



    @Override
    protected String doInBackground(String... params) {
    ms.receive(dp);
    String s = new String(dp.getData(),0,dp.getLength());

    return(s)
    }




    protected void onPostExecute(String result) {
    // execution of result of Long time consuming operation
    Seconds.setText(result);
    }
    }



    Because for getting fresh data and displaying in textbox every time i had to execute the runner in while loop which was creating problem
    .

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: The application has stopped responding

    Post your whole AsyncTask in code tags.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. unfortunately application has stopped
    By Emmarock in forum Entertainment
    Replies: 3
    Last Post: 07-08-2013, 07:01 PM
  2. JPopupMenu not responding
    By FlyNn in forum New To Java
    Replies: 3
    Last Post: 08-09-2011, 04:19 PM
  3. Replies: 1
    Last Post: 08-05-2009, 02:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •