簡體   English   中英

活動啟動后應用程序崩潰

[英]Activity crashes the app after it starts

我想先運行“ FakeActivity ”類而不是“ MainActivity ”,但每次運行它時它都會崩潰。 誰能告訴我為什么每次加載活動時都會崩潰。 以下是我的Java代碼:

package com.example.android.findbar;

import android.location.Location;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class FakeActivity extends AppCompatActivity {

    /*Global variables for user's Longitude and User's Latitude*/

    EditText Longitude, Latitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fake);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Longitude = (EditText)findViewById(R.id.Longitude);
        Latitude = (EditText)findViewById(R.id.Latitude);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

    }

    public void CheckLocation(View view){

        String Longi = Longitude.getText().toString();
        String Lati = Latitude.getText().toString();
        String type = "Locator";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, Longi, Lati);
    }

}

然后是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.android.findbar.FakeActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_fake" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

然后我的清單文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.findbar">

    <uses-permission android:name="android.permission.INTERNET" />

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

        </activity>

        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
        <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="@string/fb_login_protocol_scheme" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps" />
        <activity android:name=".SecondActivity" />

        <activity
            android:name=".FakeActivity"
            android:label="@string/title_activity_fake"
            android:theme="@style/AppTheme.NoActionBar">

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

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

    </application>

</manifest>

邏輯貓

03-22 13:23:41.923 761-1248/? W/ActivityManager:   Force finishing activity com.example.android.findbar/.FakeActivity
03-22 13:23:42.046 1943-1958/? W/GooglePlayServicesUtil: Google Play services is missing.
03-22 13:23:42.048 761-772/? W/ActivityManager: Unable to start service Intent { act=com.google.android.gms.ads.identifier.service.START pkg=com.google.android.gms } U=0: not found
03-22 13:23:42.134 761-1827/? I/OpenGLRenderer: Initialized EGL, version 1.4
03-22 13:23:42.135 761-1827/? D/OpenGLRenderer: Swap behavior 1
03-22 13:23:42.230 761-783/? I/Choreographer: Skipped 40 frames!  The application may be doing too much work on its main thread.
03-22 13:23:42.435 761-776/? W/ActivityManager: Activity pause timeout for ActivityRecord{dfb089c u0 com.example.android.findbar/.FakeActivity t4 f}
03-22 13:23:42.487 761-776/? I/WindowManager: Failed to capture screenshot of Token{52a62a5 ActivityRecord{dfb089c u0 com.example.android.findbar/.FakeActivity t4 f}} appWin=Window{e6b5ad2 u0 Starting com.example.android.findbar} drawState=3
03-22 13:23:42.640 761-783/? I/WindowManager: Destroying surface Surface(name=Starting com.example.android.findbar) called by com.android.server.wm.WindowStateAnimator.destroySurface:2014 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:881 com.android.server.wm.WindowState.removeLocked:1449 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2478 com.android.server.wm.WindowManagerService.removeWindowLocked:2436 com.android.server.wm.WindowManagerService.removeWindowLocked:2305 com.android.server.wm.WindowManagerService.removeWindow:2300 com.android.server.wm.Session.remove:193 
03-22 13:23:42.932 761-777/? I/Choreographer: Skipped 39 frames!  The application may be doing too much work on its main thread.
03-22 13:23:44.635 761-1248/? W/ActivityManager:   Force finishing activity com.example.android.findbar/.FakeActivity
03-22 13:23:44.635 761-1248/? I/ActivityManager: Killing 1943:com.example.android.findbar/u0a63 (adj 900): crash
03-22 13:23:44.667 761-777/? W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
03-22 13:23:44.669 761-1248/? W/InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@21574e8 attribute=null, token = android.os.BinderProxy@6d4495
03-22 13:23:45.008 761-783/? I/WindowManager: Destroying surface Surface(name=Application Error: com.example.android.findbar) called by com.android.server.wm.WindowStateAnimator.destroySurface:2014 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:881 com.android.server.wm.WindowState.destroyOrSaveSurface:2073 com.android.server.wm.WindowSurfacePlacer.performSurfacePlacementInner:429 com.android.server.wm.WindowSurfacePlacer.performSurfacePlacementLoop:232 com.android.server.wm.WindowSurfacePlacer.performSurfacePlacement:180 com.android.server.wm.WindowManagerService$H.handleMessage:8079 android.os.Handler.dispatchMessage:102 
03-22 13:23:45.010 761-783/? W/AppOps: Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
03-22 13:23:51.792 482-499/? W/AudioFlinger: write blocked for 10367 msecs, 5 delayed writes, thread 0xf14032c0
03-22 13:25:07.227 485-715/? E/Netd: Failed to dump IPv4 sockets for UID: No such file or directory
03-22 13:25:07.228 761-775/? E/NetworkManagement: Error closing sockets after enabling chain standby: android.os.ServiceSpecificException: destroySockets: No such file or directory
03-22 13:25:38.127 761-809/? W/WifiMode: WiredSSID, Invalid SupportedRates!!!
03-22 13:25:38.128 761-811/? W/WifiMode: WiredSSID, Invalid SupportedRates!!!
03-22 13:25:38.129 761-809/? W/WifiMode: WiredSSID, Invalid SupportedRates!!!
01-01 00:00:00.000 0-0/? E/Internal: device '192.168.63.101:5555' not found

首先,您需要檢查您的 SDK 中是否可以使用 Google Play 服務。 因為您的崩潰報告在您的 Logcat 的第二行中顯示了“缺少 Google Play 服務”的錯誤。

在 Android Studio 中單擊:工具 > Android > SDK 管理器 > 啟動獨立 sdk 管理器。

查找:附加 > Google Play 服務。

如果未安裝 Google Play 服務,請先安裝它,然后再次運行項目。

  1. 更換所有Longitude ,以longitude和所有LatitudelatitudeXML文件和Java文件。 它會解決你的問題。
EditText longitude, latitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fake);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    longitude = (EditText)findViewById(R.id.longitude);
    latitude = (EditText)findViewById(R.id.latitude);
  1. 也按照上面的JAVA代碼更改EditText id

/* 添加清單文件 ---- 在應用程序標簽內 - */

    android:usesCleartextTraffic="true"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM