Applications stops working when trying to call AlertDialog

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 4219 Views - Last Post: 20 October 2011 - 02:08 AM

#1 TheCompBoy   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 09:37 AM

When the user is releasing their finger from the screen im intrested in making this dialog show up but im getting the message when i run the application that the application stopped working and i can't figure out why its not working..

Note: onTouchEvent is inside its own class its not in the main class and also the AlertDialog is its own class as you may see when its Extending AlertDialog.

Here is the code for the alert dialog:

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PlacepinDialog extends AlertDialog{

    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    TextView text;
    ImageView image;
    Button place;
    Button cancel;
    LayoutInflater inflater;
    View layout;

    public PlacepinDialog(Context context) {
        super(context);
        //Setting up View and Inflater
        LayoutInflater inflater = (LayoutInflater)getLayoutInflater();
        View layout = inflater.inflate(R.layout.placepin_dialog,
                (ViewGroup) findViewById(R.id.mvMain));

        //Text Views
        TextView text = (TextView) layout.findViewById(R.id.Placetext);
        text.setText("Do you want to place a pin at the location you pressed?");

        //Image Views
        ImageView image = (ImageView) layout.findViewById(R.id.Placeimage);
        image.setImageResource(R.drawable.icon);

        //Building the Dialog
        builder = new AlertDialog.Builder(context);
        builder.setView(layout);
        alertDialog.setTitle("Place Pin");
        alertDialog = builder.create();
    }
    }


here is where the alertdialog gets called(this onTouchEvent is in another class than the main class so i can't just do main.this):

public boolean onTouchEvent(MotionEvent e, MapView mv){
        int i = e.getAction();
        Context c;

        switch(i){

        case MotionEvent.ACTION_DOWN:
            //When your finger touches the screen
            Log.d("ACTION DOWN", "Finger touched screen");

            break;

        case MotionEvent.ACTION_UP:
            //When your finger stop touching the screen
            Log.d("ACTION UP", "Finger was removed from screen");
            try{
            PlacepinDialog alertDialog = new PlacepinDialog(c);
            }
            catch(){

            }
            break;

        case MotionEvent.ACTION_MOVE:
            //When your finger moves around the screen
            Log.d("ACTION MOVE", "Finger was moved around the screen");

            break;
        }

        return false;
    }


Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Applications stops working when trying to call AlertDialog

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,924
  • Joined: 27-December 08

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 10:07 AM

If the application stops working, then there has to be an error message in the LogCat. Please post that.
Was This Post Helpful? 0
  • +
  • -

#3 TheCompBoy   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 10:20 AM

I get this long message in logcat:

10-17 17:19:14.892: DEBUG/AndroidRuntime(310): Shutting down VM
10-17 17:19:14.932: WARN/dalvikvm(310): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): FATAL EXCEPTION: main
10-17 17:19:15.042: ERROR/AndroidRuntime(310): java.lang.NullPointerException
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.app.Dialog.<init>(Dialog.java:141)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.app.AlertDialog.<init>(AlertDialog.java:63)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.app.AlertDialog.<init>(AlertDialog.java:59)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at my.locations.oskar.mendel.PlacepinDialog.<init>(PlacepinDialog.java:24)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at my.locations.oskar.mendel.TouchOverlay.onTouchEvent(TouchOverlay.java:31)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at com.google.android.maps.MapView.onTouchEvent(MapView.java:643)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.view.View.dispatchTouchEvent(View.java:3766)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at 0-17 17:19:14.892: DEBUG/AndroidRuntime(310): Shutting down VM
10-android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(Phonewindow.java:1671)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at com.android.internal.policy.impl.Phonewindow.superDispatchTouchEvent(Phonewindow.java:1107)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(Phonewindow.java:1655)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.os.Looper.loop(Looper.java:123)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at java.lang.reflect.Method.invoke(Method.java:521)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-17 17:19:15.042: ERROR/AndroidRuntime(310): at dalvik.system.NativeStart.main(Native Method)
10-17 17:19:15.193: WARN/ActivityManager(59): Force finishing activity my.locations.oskar.mendel/.MainActivity
10-17 17:19:15.712: INFO/ARMAssembler(59): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x37d910:0x37d9cc] in 771397 ns
10-17 17:19:15.742: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{450c02a0 my.locations.oskar.mendel/.MainActivity}
10-17 17:19:26.295: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{450c02a0 my.locations.oskar.mendel/.MainActivity}

This post has been edited by TheCompBoy: 17 October 2011 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,924
  • Joined: 27-December 08

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 10:24 AM

Look in your onTouchEvent() method. You declare a local variable Context and you don't initialize it. If your Listener is in another class, it will have to accept the Context from the Activity that the PlacepinDialog will be displayed on. Right now, the Context is null, so you are passing null to the constructor of the PlacepinDialog. Hence the NullPointerException.

Context c;
...
try{
   PlacepinDialog alertDialog = new PlacepinDialog(c);
}
catch(){

}


Was This Post Helpful? 0
  • +
  • -

#5 TheCompBoy   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 10:44 AM

This dialog is going to be displayed on the Main Activity so, Would it work to call getContext() to know what context it should be equal?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,924
  • Joined: 27-December 08

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 10:45 AM

Yes- getContext() sounds like a safe bet.
Was This Post Helpful? 0
  • +
  • -

#7 TheCompBoy   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 11:11 AM

How would i use it? c = something.getContext(); If its correct what should i replace "Something" with ?

Couldn't figure it out..

This post has been edited by TheCompBoy: 17 October 2011 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,924
  • Joined: 27-December 08

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 11:12 AM

If "something" is your Activity class, you can pass "something" directly as the Context. If it is a View, you can invoke getContext() on it.
Was This Post Helpful? 0
  • +
  • -

#9 TheCompBoy   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 11:18 AM

If i understand you correctly here i would be able to do C = mainactivity; if thats the Activity class im using?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,924
  • Joined: 27-December 08

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 11:20 AM

Only if mainactivity is an Object.
Was This Post Helpful? 0
  • +
  • -

#11 TheCompBoy   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 11:21 AM

MainActivity is the class name

EDIT:
I just read this: http://journals.ecs....cts/object.html
MainActivity is not an Object

This post has been edited by TheCompBoy: 17 October 2011 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,924
  • Joined: 27-December 08

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 11:28 AM

If your Activity is set up like this:
class MainActivity extends Activity{

    private class TouchHandler implements OnTouchListener{

       public boolean onTouchEvent(MotionEvent e, MapView mv){
           ..code.. 
 
           //MainActivity.this references the Activity, not the TouchHandler
           PlacepinDialog alertDialog = new PlacepinDialog(MainActivity.this);
       }
    }
}


Was This Post Helpful? 0
  • +
  • -

#13 TheCompBoy   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 11:31 AM

Well my MainActivity for me is just having the onCreate and onpause and the basic stuff.. my onTouchEvent is in another class that is extending Overlay..

How would i get the context in this case?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,924
  • Joined: 27-December 08

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 11:31 AM

Post all your code so I can see the setup.
Was This Post Helpful? 0
  • +
  • -

#15 TheCompBoy   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Applications stops working when trying to call AlertDialog

Posted 17 October 2011 - 11:37 AM

Here is MainActivity, Did you want all the classes??:
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends MapActivity implements LocationListener {
    /** Called when the activity is first created. */
	
	MapView mainMap;
	MapController mapController;
	boolean mapViewControll;
	LocationManager locationManager;
	GeoPoint userLocation;
	Drawable userIcon;
	List<Overlay> overlayList;
	TouchOverlay touchOverlay = new TouchOverlay();
	double lat;
	double lng;
	OverlayItem usersLocationIcon;
	LocationPin previousLocation;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Adding features for the map
        mainMap = (MapView)findViewById(R.id.mvMain);
        mainMap.setBuiltInZoomControls(true);
        mainMap.setTraffic(true);
        mapViewControll = true;
        mapController = mainMap.getController();
        
        //Adding the other classes
        overlayList = mainMap.getOverlays();
        overlayList.add(touchOverlay);
        
        //LocationListener and manager stuff here!
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 200.0f, this);
        
        //Images
        userIcon = getResources().getDrawable(R.drawable.icon);
        
    }
    

    //Adding a menu when the phones menu button was pressed this gets called.
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater myMenu = getMenuInflater();
		myMenu.inflate(R.menu.location_menu, menu);
		return true;
	}
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch(item.getItemId()){
		
		//If the changeView button was pressed
		case R.id.changeView:
			if(mapViewControll == true){
				mainMap.setTraffic(false);
				mainMap.setSatellite(true);
				mapViewControll = false;
		    }
			else if(mapViewControll == false){
				mainMap.setSatellite(false);
				mainMap.setTraffic(true);
				mapViewControll = true;
			}
			break;
			
		//if the help button was pressed
		case R.id.help:
			break;
			
		}
		
		return super.onOptionsItemSelected(item);
	}


	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}


	//-----------------------------------------------------------------------
	//LocationListeners methods
	//
	//These methods is needed from the LocationListener that was
	//Implemented.
	//-----------------------------------------------------------------------
	
	public void onLocationchanged(Location location) {
		if (location != null) {
			
			//Gets users longitude and latitude
			lat = location.getLatitude();
			lng = location.getLongitude();
			
			//sets the GeoPoint usersLocation equal lat and lng
			userLocation = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
			
			OverlayItem usersLocationIcon = new OverlayItem(userLocation, null, null);
			LocationPin myLocationPin = new LocationPin(userIcon, MainActivity.this);
			
			//Removes the previous location
			if(previousLocation != null)
				   mainMap.getOverlays().remove(previousLocation); 
			
			myLocationPin.getLocation(usersLocationIcon);
			overlayList.add(myLocationPin);
			
			//refresh the map
			mainMap.invalidate(); 
			
			//Making myLocationPin into the previousLocation just to be able to remove it later
			previousLocation = myLocationPin;
		}
		
	}


	public void onProviderDisabled(String provider) {
		
	}


	public void onProviderEnabled(String provider) {
		
	}


	public void onStatusChanged(String provider, int status, Bundle extras) {
		
	}
}



Here's TouchOverlay:
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;

import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class TouchOverlay extends Overlay{
	
	long Start;
	long Stop;
	PlacepinDialog alertDialog;
	Context c;
	
	public boolean onTouchEvent(MotionEvent e, MapView mv){
		int i = e.getAction();
		c = MainActivity.getContext();
	
		switch(i){
		
		case MotionEvent.ACTION_DOWN:
			//When your finger touches the screen
			Log.d("ACTION DOWN", "Finger touched screen");
			
			break;
			
		case MotionEvent.ACTION_UP:
			//When your finger stop touching the screen
			Log.d("ACTION UP", "Finger was removed from screen");
			PlacepinDialog alertDialog = new PlacepinDialog(c);
			
			break;
			
		case MotionEvent.ACTION_MOVE:
			//When your finger moves around the screen
			Log.d("ACTION MOVE", "Finger was moved around the screen");
			
			break;
		}
		
		return false;
	}
}



Heres PlacePinDialog:
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PlacepinDialog extends AlertDialog{
	
	AlertDialog.Builder builder;
	AlertDialog alertDialog;
	TextView text;
	ImageView image;
	Button place;
	Button cancel;
	LayoutInflater inflater;
	View layout;
	
	public PlacepinDialog(Context context) {
		super(context);
		//Setting up View and Inflater
		LayoutInflater inflater = (LayoutInflater)getLayoutInflater();
		View layout = inflater.inflate(R.layout.placepin_dialog,
                (ViewGroup) findViewById(R.id.mvMain));
		
		//Text Views
		TextView text = (TextView) layout.findViewById(R.id.Placetext);
		text.setText("Do you want to place a pin at the location you pressed?");
		
		//Image Views
		ImageView image = (ImageView) layout.findViewById(R.id.Placeimage);
		image.setImageResource(R.drawable.icon);
		
		//Building the Dialog
		builder = new AlertDialog.Builder(context);
		builder.setView(layout);
		alertDialog.setTitle("Place Pin");
		alertDialog = builder.create();
	}
}


Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3