简体   繁体   中英

Android on button click App crashes

In Main Activity i'm using Intent as

btnlocate.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) {
              Intent intent = new Intent(MainActivity.this, gotomap.class);
                 startActivity(intent);
                 MainActivity.this.finish();
        }           
    }); 

In gotomap.xml Im using fragment in layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

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

</RelativeLayout>

gotomap.java

public class gotomap extends Activity {
    public gotomap(){}
    getcoordinates loc;
    double lati,longi;
       private GoogleMap googleMap;

       @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          loc = new getcoordinates(this);               //getlocation

            if(loc.canGetLocation())            
            {                
               lati = loc.getLatitude();
               longi = loc.getLongitude();
              Toast.makeText(this, "Your Location is - \nLat: " + lati + "\nLong: " + longi, Toast.LENGTH_LONG).show();    
          }
            final LatLng locate = new LatLng(lati ,longi);

          setContentView(R.layout.gotomap);

          try {
             if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().
                findFragmentById(R.id.map)).getMap();
             }

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

log

  02-02 10:16:13.986: I/Process(30530): Sending signal. PID: 30530 SIG: 9
02-02 10:16:17.883: V/Monotype(30591): SetAppTypeFace- try to flip, app = com.example.locato
02-02 10:16:17.885: V/Monotype(30591):     Typeface getFontPathFlipFont - systemFont = default#default
02-02 10:16:17.891: V/Monotype(30591): SetAppTypeFace- try to flip, app = com.example.locato
02-02 10:16:17.891: V/Monotype(30591):     Typeface getFontPathFlipFont - systemFont = default#default
02-02 10:16:17.943: D/OpenGLRenderer(30591): Render dirty regions requested: true
02-02 10:16:17.963: D/Atlas(30591): Validating map...
02-02 10:16:18.041: I/OpenGLRenderer(30591): Initialized EGL, version 1.4
02-02 10:16:18.041: W/OpenGLRenderer(30591): Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
02-02 10:16:18.069: D/OpenGLRenderer(30591): Enabling debug mode 0
02-02 10:16:19.237: I/System.out(30591): intent>>>>>>>>Intent { cmp=com.example.locato/.gotomap }
02-02 10:16:19.238: D/AndroidRuntime(30591): Shutting down VM
02-02 10:16:19.239: E/AndroidRuntime(30591): FATAL EXCEPTION: main
02-02 10:16:19.239: E/AndroidRuntime(30591): Process: com.example.locato, PID: 30591
02-02 10:16:19.239: E/AndroidRuntime(30591): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.locato/com.example.locato.gotomap}; have you declared this activity in your AndroidManifest.xml?
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.app.Activity.startActivityForResult(Activity.java:3778)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.app.Activity.startActivityForResult(Activity.java:3739)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.app.Activity.startActivity(Activity.java:4049)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.app.Activity.startActivity(Activity.java:4017)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at com.example.locato.MainActivity$1.onClick(MainActivity.java:24)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.view.View.performClick(View.java:4756)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.view.View$PerformClick.run(View.java:19761)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.os.Handler.handleCallback(Handler.java:739)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.os.Looper.loop(Looper.java:135)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at android.app.ActivityThread.main(ActivityThread.java:5253)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at java.lang.reflect.Method.invoke(Native Method)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at java.lang.reflect.Method.invoke(Method.java:372)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
02-02 10:16:19.239: E/AndroidRuntime(30591):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

Breakpoint doesnt even go to the send activity,,, Application crashes while calling the intent... Please do the needful...

您必须在清单中声明您的活动,例如:

<activity name="com.example.locato.gotomap"/>

First of all, you need to make sure the "gotomap" activity is defined in the manifest file. Second the activity class first letter should be capital. This is general Java standard.

 <activity
            android:name=".gotomap">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

Make sure each of your activity should be mention in manifest as bellow

<activity name="com.example.locato.gotomap"/> 

// use java naming conventions class name should be capital.

Note: To improve your coding standard use java naming conventions like class name should be start Capital

将AndroidManifest.xml中的gotomap.class条目作为活动提供。

<activity android:name=".gotomap" />

将此添加到<application>标记内的清单中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM