malik farhan malik farhan - 2 months ago 23
Android Question

"Unfortunately app has stopped Error" Android Studio

I want to open my MapsActivity on notification click notification is working properly but when i click on notification it forse me to stopped app

NotificationActivity.class

public class NotificationActivity extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);

b1=(Button)findViewById(R.id.buttonnot);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Notify("You've received new message","");
}
});
}
private void Notify(String notificationTitle, String notificationMessage){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
@SuppressWarnings("deprecation")

Notification notification = new Notification(R.drawable.ic_launcher,"New Message", System.currentTimeMillis());
Intent notificationIntent = new Intent(this,MapsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

notification.setLatestEventInfo(NotificationActivity.this, notificationTitle,notificationMessage, pendingIntent);
notificationManager.notify(9999, notification);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


MapsAcivity.class

public class MapActivity extends Activity {


// GoogleMap googleMap;
final LatLng TutorialsPoint = new LatLng(33.5493325, 73.123826);

float zoomLevel = (float) 16.0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
// add here

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync((OnMapReadyCallback) this);


@Override
public void onMapReady(final GoogleMap googleMap) // Give Error on this line "Error:(36, 31) error: ';' expected"
{

try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(TutorialsPoint, zoomLevel));


Marker TP = googleMap.addMarker(new MarkerOptions().
position(TutorialsPoint).title("TutorialsPoint"));


} catch (Exception e) {
e.printStackTrace();
}

}

}
}


activity_map

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MapActivity">

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/activity_map" />

</RelativeLayout>


LogCat


at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:148) at
android.app.ActivityThread.main(ActivityThread.java:5417) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #12:
Binary XML file line #12: Error inflating class fragment at
com.example.dell.hamad.MapActivity.onCreate(MapActivity.java:25) at
android.app.Activity.performCreate(Activity.java:6237) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.main(ActivityThread.java:5417)  at
java.lang.reflect.Method.invoke(Native Method)  at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.view.InflateException: Binary XML file line #12:
Error inflating class fragment

Answer

So, I just found the error

Intent notificationIntent = new Intent(this,MapsActivity.class);

Solved code

public class NotificationActivity extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);

    b1=(Button)findViewById(R.id.buttonnot);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Notify("You've received new message","");
        }
    });
}
private void Notify(String notificationTitle, String notificationMessage){
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    @SuppressWarnings("deprecation")

    Notification notification = new Notification(R.drawable.ic_launcher,"New Message", System.currentTimeMillis());

    **// Error due to Intent.....**

    Intent notificationIntent = new Intent(this,MapsActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

    notification.setLatestEventInfo(NotificationActivity.this, notificationTitle,notificationMessage, pendingIntent);
    notificationManager.notify(9999, notification);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

On MapsActivity add SupportMapFragment and override onMapReady method and perform all map work in there:

public class MapActivity extends Activity implements OnMapReadyCallback{


// GoogleMap googleMap;
final LatLng TutorialsPoint = new LatLng(33.5493325, 73.123826);

float zoomLevel = (float) 16.0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    // add here

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    try {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        }
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(TutorialsPoint, zoomLevel));


        Marker TP = googleMap.addMarker(new MarkerOptions().
                position(TutorialsPoint).title("TutorialsPoint"));


    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
Comments