【问题标题】:Android on button click App crashesAndroid上的按钮单击应用程序崩溃
【发布时间】:2016-02-02 04:42:02
【问题描述】:

在主要活动中,我使用 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)

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

【问题讨论】:

  • gotomap 类中删除 public gotomap(){}。你不应该不必要地创建构造函数。
  • 崩溃有哪些异常?请将堆栈跟踪添加到问题中。
  • 还要确保您在清单中定义了gotomap
  • 我删除了 Public gotomap(){} 但同样的错误
  • 将您的活动添加到清单文件“com.example.locato.gotomap”

标签: android google-maps android-intent


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    确保您的每个活动都应在清单中提及,如下所示

    <activity name="com.example.locato.gotomap"/> 
    
    // use java naming conventions class name should be capital.
    

    注意:为了提高您的编码标准,请使用 java 命名约定,如类名应该以大写开头

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        首先,您需要确保在清单文件中定义了“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>
        

        【讨论】:

          【解决方案5】:
          <activity android:name=".gotomap" />
          

          将此添加到您的清单里面&lt;application&gt;标签

          【讨论】:

            猜你喜欢
            • 2017-02-14
            • 1970-01-01
            • 1970-01-01
            • 2017-03-15
            • 2021-02-15
            • 1970-01-01
            相关资源
            最近更新 更多