Android按钮单击应用程序崩溃

时间:2016-02-02 04:42:03

标签: android google-maps android-intent

在主要活动中,我使用Intent作为

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

在gotomap.xml中我在布局中使用片段

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

日志

  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)

断点甚至没有去发送活动,,, 调用intent时应用程序崩溃... 请做必要的......

5 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

首先,您需要确保清单文件中定义了“gotomap”活动。其次,活动类首字母应该是资本。这是一般的Java标准。

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

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

答案 2 :(得分:0)

确保您的每项活动都应在清单中提及

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

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

注意:要提高编码标准,请使用java命名约定,例如类名称应该是start Capital

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

将此内容添加到<application>代码

中的清单中