应用程序在模拟器上运行但在移动设备上停止

时间:2015-11-03 08:55:35

标签: android

我正在尝试阅读我的GPS位置。它适用于模拟器Nexus S API 23 但是当它安装在具有4.1.1或5.0.1版本的Deices上时,它在工作之前停止了。我需要知道,如果问题是因为代码或因为设备之间的不同版本: 这是我的代码:

   Main Activity.java
        package com.example.user.getgps1;

        import android.Manifest;
        import android.annotation.TargetApi;
        import android.app.ProgressDialog;
        import android.content.Context;
        import android.content.pm.PackageManager;
        import android.location.Location;
        import android.location.LocationManager;
        import android.os.Build;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.TextView;

        public class MainActivity extends AppCompatActivity {
            static final long meter=1;
            static final long mill=1000;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                LocationManager manager=(LocationManager) this.getSystemService(context.LOCATION_SERVICE);
                if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED||checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, meter, mill, new locationlist(this));
                }
                manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,meter,mill,new locationlist(this));
            }
            ProgressDialog dialog;
            Context context;
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            public void bulocation(View view) {
                context = this;
                dialog = new ProgressDialog(MainActivity.this);
                dialog.show();
                dialog.setMessage("Getting Last Location");
                LocationManager locationmanager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

                if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED||checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, meter, mill, new locationlist(this));
                }
                Location lastknown = locationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if(lastknown==null)
                    lastknown=locationmanager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
                TextView t1=(TextView) findViewById(R.id.textView1);
                TextView t2=(TextView) findViewById(R.id.textView2);
                t2.setText(Double.toString(lastknown.getLatitude()));
                t1.setText(Double.toString(lastknown.getLongitude()));
                dialog.dismiss();
            }
        }

activity_main 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView1"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="bulocation"/>

</RelativeLayout>

Android_manifest

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

    <uses-permission android:name="android.permission.INTERNET"/>
    <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:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

    locationlist.java
    package com.example.user.getgps1;

    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.os.Bundle;
    import android.widget.Toast;

    public class locationlist implements LocationListener {
        Context context;
        public locationlist(Context context)
        {
            this.context=context;
        }
        @Override
        public void onLocationChanged(Location location) {
            Toast.makeText(context,"log:"+Double.toString(location.getLongitude())+", lat: "+Double.toString(location.getLatitude()),Toast.LENGTH_LONG).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(context,"GPS is Changed",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(context,"GPS is on",Toast.LENGTH_LONG).show();

        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(context,"GPS is OFF",Toast.LENGTH_LONG).show();

        }
    }

what is the problem

1 个答案:

答案 0 :(得分:0)

查看您发布的logcat作为答案。

您对getLastKnownLocation()的调用返回null。

在真实的设备上,你不能假设总是得到一个位置,

希望这有帮助。