通过后退按钮返回主页活动时应用程序崩溃

时间:2015-03-29 12:32:53

标签: android

我创建了按钮以回到家庭活动但是当我按下这个按钮时它变得不可思议然后它运作良好我不明白为什么但我希望这个错误不会出现并且 这是我的代码

homeactivty

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 ofbutton

<Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="goback"
            android:text="@string/Back" />

logcat是

03-29 08:47:46.843: E/AndroidRuntime(3540): FATAL EXCEPTION: main
03-29 08:47:46.843: E/AndroidRuntime(3540): Process: com.example.gmapsapp, PID: 3540
03-29 08:47:46.843: E/AndroidRuntime(3540): java.lang.IllegalStateException: Could not find a method goback(View) in the activity class com.example.gmapsapp.MainActivity for onClick handler on view class android.widget.Button with id 'button2'
03-29 08:47:46.843: E/AndroidRuntime(3540):     at android.view.View$1.onClick(View.java:3810)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at android.view.View.performClick(View.java:4438)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at android.view.View$PerformClick.run(View.java:18422)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at android.os.Handler.handleCallback(Handler.java:733)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at android.os.Looper.loop(Looper.java:136)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at java.lang.reflect.Method.invoke(Method.java:515)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at dalvik.system.NativeStart.main(Native Method)
03-29 08:47:46.843: E/AndroidRuntime(3540): Caused by: java.lang.NoSuchMethodException: goback [class android.view.View]
03-29 08:47:46.843: E/AndroidRuntime(3540):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at java.lang.Class.getMethod(Class.java:857)
03-29 08:47:46.843: E/AndroidRuntime(3540):     at android.view.View$1.onClick(View.java:3803)
03-29 08:47:46.843: E/AndroidRuntime(3540):     ... 11 more

按钮布局

<?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>
抱歉英语不好

2 个答案:

答案 0 :(得分:0)

当您为View声明android:onClick属性时,使用该布局的Activity必须提供具有以下签名的方法:

public void nameSetInOnClick(View view) {

}

在您的情况下,您有android:onClick="goback",因此您必须提供一个名为public的{​​{1}}方法,该方法仅作为参数 一个View对象:< / p>

goback

答案 1 :(得分:0)

包含后退按钮的第二个活动应该有这样的方法:

public void goback(View v){

}

第二个活动的xml文件应该像这样设置 android:onClick

<Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="goback"
            android:text="@string/Back" />