Android 通过“后退”按钮返回主活动时应用程序崩溃
我创建了“返回主页”活动的按钮,但当我按下此按钮时,不幸的是,它工作正常,我不明白为什么,但我希望此错误不会出现,并且 这是我的密码 家庭活动Android 通过“后退”按钮返回主活动时应用程序崩溃,android,Android,我创建了“返回主页”活动的按钮,但当我按下此按钮时,不幸的是,它工作正常,我不明白为什么,但我希望此错误不会出现,并且 这是我的密码 家庭活动 package com.example.gmapsapp; import android.os.Bundle; import android.content.Intent; import android.view.View; public class homepage extends MainActivity { public void on
package com.example.gmapsapp;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
public class homepage extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
}
public void onClick(View view)
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void onClick1(View view)
{
Intent intent = new Intent(this, Airports.class);
startActivity(intent);
}
public void goback(View view)
{
Intent intent = new Intent(this, homepage.class);
startActivity(intent);
}
}
包含后退按钮的第二个活动
package com.example.gmapsapp;
import java.io.IOException;
import java.util.List;
import android.app.Dialog;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap mMap;
private static final double NASRCITY_LAT = 30.066108,
NASRCITY_LNG =31.336184;
private static final float DEFAULTZOOM = 13;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
setContentView(R.layout.activity_map);
if (initMap()) {
gotoLocation(NASRCITY_LAT, NASRCITY_LNG, DEFAULTZOOM);
}
else {
Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
}
}
else {
setContentView(R.layout.activity_main);
}
}
@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;
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
else {
Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFrag =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFrag.getMap();
}
if (mMap != null){
setupmap();
}
return (mMap != null);
}
private void setupmap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(30.060953, 31.357416)).title("Ahmed Hendy Pharmacy"));
mMap.addMarker(new MarkerOptions().position(new LatLng(30.056645, 31.354756)).title("Hasan Ali Pharmacy"));
mMap.addMarker(new MarkerOptions().position(new LatLng(30.051739, 31.355807)).title("Osama El-Meleegi's Pharmacy"));
mMap.addMarker(new MarkerOptions().position(new LatLng(30.054673, 31.346751)).title("Ezaby Pharmacies"));
mMap.addMarker(new MarkerOptions().position(new LatLng(30.054711, 31.35052)).title("pharmacies drug"));
mMap.addMarker(new MarkerOptions().position(new LatLng(30.045609, 31.361686)).title("Maraghy tenth district"));
mMap.addMarker(new MarkerOptions().position(new LatLng(30.040400, 31.329994)).title("Anji Pharmacy"));
mMap.addMarker(new MarkerOptions().position(new LatLng(30.064593, 31.339916)).title("Amr Pharmacy"));
mMap.addMarker(new MarkerOptions().position(new LatLng(30.056538, 31.338285)).title("Pharmacies Delmar and Atallah"));
mMap.addMarker(new MarkerOptions().position(new LatLng(30.061886, 31.333264)).title("J. Bakl Pharmacy"));
}
@SuppressWarnings("unused")
private void gotoLocation(double lat, double lng) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
mMap.moveCamera(update);
}
private void gotoLocation(double lat, double lng,
float zoom) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
mMap.moveCamera(update);
}
public void geoLocate(View v) throws IOException {
hideSoftKeyboard(v);
EditText et = (EditText) findViewById(R.id.editText1);
String location = et.getText().toString();
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(location, 1);
Address add = list.get(0);
String locality = add.getLocality();
Toast.makeText(this, locality, Toast.LENGTH_LONG).show();
double lat = add.getLatitude();
double lng = add.getLongitude();
gotoLocation(lat, lng, DEFAULTZOOM);
}
private void hideSoftKeyboard(View v) {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
按钮布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="56dp"
android:layout_height="35dp"
android:layout_gravity="left|center_vertical"
android:layout_weight="0.80"
android:text="@string/Location"
android:textSize="18sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center|center_vertical"
android:layout_weight="0.55"
android:ems="10"
android:inputType="textAutoComplete" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:onClick="geoLocate"
android:text="@string/Go" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goback"
android:text="@string/Back" />
</LinearLayout>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</LinearLayout>
抱歉,英语不好当您为视图声明
android:onClick
属性时,使用该布局的活动必须提供具有以下签名的方法:
public void nameSetInOnClick(View view) {
}
在您的示例中,您有android:onClick=“goback”
,因此您必须提供一个名为goback
的public方法,该方法仅将视图对象作为参数:
public void goback(View view) {
}
包含“后退”按钮的第二个活动应具有如下方法:
public void goback(View v){
}
第二个活动的xml文件应该有如下设置
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goback"
android:text="@string/Back" />
我编辑了这个函数,你的意思是什么?它仍然崩溃在哪个布局中你声明了那个按钮?请在MainActivitypost中移动goback方法日志猫它和我做的一样,但它崩溃后工作正常可能内存问题或其他问题,尝试解决它。
public void goback(View v){
}
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goback"
android:text="@string/Back" />