“不幸的是app已停止错误”Android Studio

时间:2016-09-05 09:42:00

标签: android google-maps notifications

我想在通知点击通知正常工作时打开我的MapsActivity,但是当我点击通知时,我觉得我停止了应用

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的

  

在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)   在android.app.ActivityThread.-wrap11(ActivityThread.java)处   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)   在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)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)   引起:android.view.InflateException:二进制XML文件行#12:   二进制XML文件行#12:错误膨胀类片段   com.example.dell.hamad.MapActivity.onCreate(MapActivity.java:25)at at   android.app.Activity.performCreate(Activity.java:6237)at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)   在android.app.ActivityThread.main(ActivityThread.java:5417)处   java.lang.reflect.Method.invoke(Native Method)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)   引起:android.view.InflateException:二进制XML文件行#12:   膨胀类片段错误

1 个答案:

答案 0 :(得分:0)

所以,我刚发现错误

Intent notificationIntent = new Intent(this,MapsActivity.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());

    **// 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);
}
}

在MapsActivity上添加SupportMapFragment并覆盖onMapReady方法并执行所有地图工作:

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();
    }
}
}