简体   繁体   中英

Activity crashes the app after it starts

I want to run " FakeActivity " class first instead of " MainActivity " but it keeps crashing every time i run it. Can anyone please tell me why the activity is crashing every time I load it. Following is my Java code:

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

}

then my xml file:

<?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>

and then my manifest file :

<?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>

Logcat

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

First of all, you need to check that Google Play services is available or not in your SDK. Because your crash report shows the error that "Google Play services is missing" in second line of your Logcat.

In Android Studio click: Tools > Android > SDK Manager > Launch stand alon sdk manager.

Find : Extras > Google Play Services.

if Google Play Services is not installed, than install it first and than run project again.

  1. Replace all Longitude to longitude & all Latitude to latitude in XML file & Java File. It will fix your problem.
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. change ids of EditText also as per above JAVA code

/* 添加清单文件 ---- 在应用程序标签内 - */

    android:usesCleartextTraffic="true"

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