GPS应用程序 - 运行,但关闭按钮点击 [英] GPS app - Runs but shuts down on button click

查看:234
本文介绍了GPS应用程序 - 运行,但关闭按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个GPS应用程序,你可以得到的GPS通过点击一个按钮坐标。正如标题说,它关闭,当我点击该按钮。

我是新的Andr​​oid开发,这远远不是我的Java小知识已经得到和下面的一些Android的教程。

 进口android.app.Activity;
进口android.content.Context;
进口android.content.pm.ActivityInfo;
进口android.content.res.Configuration;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;
进口android.widget.Toast;公共类MainActivity扩展活动实现LocationListener的{    私人TextView的startLatitudeField;
    私人TextView的startLongitudeField;
    私人TextView的endLatitudeField;
    私人TextView的endLongitudeField;
    私人的LocationManager的LocationManager;
    私人LocationListener的LocationListener的;
    私人字符串供应商;
    双startLat = 0;
    双startLng = 0;
    双endLat = 0;
    双endLng = 0;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);        //设置TextViews为坐标
        startLatitudeField =(的TextView)findViewById(R.id.TextView02);
        startLongitudeField =(的TextView)findViewById(R.id.TextView04);
        endLatitudeField =(的TextView)findViewById(R.id.textView06);
        endLongitudeField =(的TextView)findViewById(R.id.textView08);        //按钮
        Button按钮=(按钮)findViewById(R.id.button1);
        button.setOnClickListener(新View.OnClickListener()
        {            @覆盖
            公共无效的onClick(视图v){                的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);
                地点= locationManager.getLastKnownLocation(供应商);                如果(startLat!= 0){
                    onLocationChanged(位置);
                }其他{
                    onLocationChanged(位置);
                    Button按钮=(按钮)findViewById(R.id.button1);
                    button.setText(给我家);
                }
            }
        });
    };
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }
    @覆盖
    公共无效onLocationChanged(地点){        如果(startLat == 0){
            startLat =(双)(location.getLatitude());
            startLng =(双)(location.getLongitude());
            startLatitudeField.setText(将String.valueOf(startLat));
            startLongitudeField.setText(将String.valueOf(startLng));
        }
        其他{            endLat =(双)(location.getLatitude());
            endLng =(双)(location.getLongitude());
            endLatitudeField.setText(将String.valueOf(endLat));
            endLongitudeField.setText(将String.valueOf(endLng));
        }    }
    @覆盖
    公共无效onProviderDisabled(字符串提供商){
        // TODO自动生成方法存根    }
    @覆盖
    公共无效onProviderEnabled(字符串提供商){
    }
    @覆盖
    公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
    }
}

以下是我的错误日志:

  12月2日至24日:57:55.580:I /流程(4655):发送信号。 PID:4655 SIG:9
12月2日至24日:58:09.664:D / AndroidRuntime(4772):关闭VM
12月2日至24日:58:09.664:W / dalvikvm(4772):主题ID = 1:螺纹未捕获的异常(组= 0x41544ba8)退出
12月2日至24日:58:09.664:E / AndroidRuntime(4772):致命异常:主要
12月2日至24日:58:09.664:E / AndroidRuntime(4772):工艺:com.example.getmehome,PID:4772
12月2日至24日:58:09.664:E / AndroidRuntime(4772):java.lang.IllegalArgumentException异常:无效的监听器:空
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在android.location.LocationManager.checkListener(LocationManager.java:1635)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在android.location.LocationManager.requestLocationUpdates(LocationManager.java:450)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在com.example.getmehome.MainActivity $ 1.onClick(MainActivity.java:57)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在android.view.View.performClick(View.java:4438)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在android.view.View $ PerformClick.run(View.java:18422)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在android.os.Handler.handleCallback(Handler.java:733)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在android.os.Handler.dispatchMessage(Handler.java:95)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在android.os.Looper.loop(Looper.java:136)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在android.app.ActivityThread.main(ActivityThread.java:5017)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在java.lang.reflect.Method.invokeNative(本机方法)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在java.lang.reflect.Method.invoke(Method.java:515)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12月2日至24日:58:09.664:E / AndroidRuntime(4772):在dalvik.system.NativeStart.main(本机方法)


解决方案

您忘了初始化提供商&安培; LocationListener的变量,这就是为什么它显示抛出:IllegalArgumentException:无效的监听器:空

我编辑了自己的code和现在工作在我的设备,

 公共类MainActivity扩展活动
{
    私人TextView的startLatitudeField;
    私人TextView的startLongitudeField;
    私人TextView的endLatitudeField;
    私人TextView的endLongitudeField;
    私人的LocationManager的LocationManager;
    私人LocationListener的LocationListener的;
    私人字符串供应商;
    双startLat = 0;
    双startLng = 0;
    双endLat = 0;
    双endLng = 0;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);        //设置TextViews为坐标
        startLatitudeField =(的TextView)findViewById(R.id.txtView1);
        startLongitudeField =(的TextView)findViewById(R.id.txtView2);
        endLatitudeField =(的TextView)findViewById(R.id.txtView3);
        endLongitudeField =(的TextView)findViewById(R.id.txtView4);        //按钮
        Button按钮=(按钮)findViewById(R.id.cmdButton);
        button.setOnClickListener(新View.OnClickListener()
        {            @覆盖
            公共无效的onClick(视图v){                的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
                LocationListener的=新MyLocationListener();
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);
                地点= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);                如果(startLat!= 0)
                {
                    locationListener.onLocationChanged(位置);
                }其他
                {
                    locationListener.onLocationChanged(位置);
                    Button按钮=(按钮)findViewById(R.id.cmdButton);
                    button.setText(给我家);
                }
            }
        });
    };
    私有类MyLocationListener实现LocationListener的
    {
        @覆盖
        公共无效onLocationChanged(地点)
        {
            // TODO自动生成方法存根
            如果(startLat == 0){
                startLat =(双)(location.getLatitude());
                startLng =(双)(location.getLongitude());
                startLatitudeField.setText(将String.valueOf(startLat));
                startLongitudeField.setText(将String.valueOf(startLng));
            }
            其他{                endLat =(双)(location.getLatitude());
                endLng =(双)(location.getLongitude());
                endLatitudeField.setText(将String.valueOf(endLat));
                endLongitudeField.setText(将String.valueOf(endLng));
            }
        }        @覆盖
        公共无效onProviderDisabled(字符串提供商){
            // TODO自动生成方法存根        }        @覆盖
        公共无效onProviderEnabled(字符串提供商){
            // TODO自动生成方法存根        }        @覆盖
        公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
            // TODO自动生成方法存根        }
    }
}

I'm making a GPS app where you can get the GPS coordinates by hitting a button. As stated in the title it shuts down when I click the button.

I'm new to Android development and have got this far from my little knowledge of Java and following some Android tutorials.

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {

    private TextView startLatitudeField;
    private TextView startLongitudeField;
    private TextView endLatitudeField;
    private TextView endLongitudeField;
    private LocationManager locationManager;
    private LocationListener locationListener;
    private String provider;
    double startLat = 0;
    double startLng = 0;
    double endLat = 0;
    double endLng = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //Setting TextViews for the coordinates
        startLatitudeField = (TextView) findViewById(R.id.TextView02);
        startLongitudeField = (TextView) findViewById(R.id.TextView04);
        endLatitudeField = (TextView) findViewById(R.id.textView06);
        endLongitudeField = (TextView) findViewById(R.id.textView08);



        //Button
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {

                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                Location location = locationManager.getLastKnownLocation(provider);

                if (startLat != 0) {
                    onLocationChanged(location);
                } else {
                    onLocationChanged(location);
                    Button button = (Button) findViewById(R.id.button1);
                    button.setText("Get me home");
                }
            }
        }); 
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onLocationChanged(Location location) {

        if (startLat == 0) {
            startLat = (double) (location.getLatitude());
            startLng = (double) (location.getLongitude());
            startLatitudeField.setText(String.valueOf(startLat));
            startLongitudeField.setText(String.valueOf(startLng));
        } 
        else {

            endLat = (double) (location.getLatitude());
            endLng = (double) (location.getLongitude());
            endLatitudeField.setText(String.valueOf(endLat));
            endLongitudeField.setText(String.valueOf(endLng));
        }

    }
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onProviderEnabled(String provider) {


    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {


    }


}

Following is my Error log:

    02-24 12:57:55.580: I/Process(4655): Sending signal. PID: 4655 SIG: 9
02-24 12:58:09.664: D/AndroidRuntime(4772): Shutting down VM
02-24 12:58:09.664: W/dalvikvm(4772): threadid=1: thread exiting with uncaught exception (group=0x41544ba8)
02-24 12:58:09.664: E/AndroidRuntime(4772): FATAL EXCEPTION: main
02-24 12:58:09.664: E/AndroidRuntime(4772): Process: com.example.getmehome, PID: 4772
02-24 12:58:09.664: E/AndroidRuntime(4772): java.lang.IllegalArgumentException: invalid listener: null
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.location.LocationManager.checkListener(LocationManager.java:1635)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:450)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at com.example.getmehome.MainActivity$1.onClick(MainActivity.java:57)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.view.View.performClick(View.java:4438)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.view.View$PerformClick.run(View.java:18422)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.os.Handler.handleCallback(Handler.java:733)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.os.Looper.loop(Looper.java:136)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at java.lang.reflect.Method.invoke(Method.java:515)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at dalvik.system.NativeStart.main(Native Method)

解决方案

You forgot to initialize provider & locationListener variable that's why it is showing IllegalArgumentException: invalid listener: null

I have edited your code and Now it is working in my device,

public class MainActivity extends Activity 
{
    private TextView startLatitudeField;
    private TextView startLongitudeField;
    private TextView endLatitudeField;
    private TextView endLongitudeField;
    private LocationManager locationManager;
    private LocationListener locationListener;
    private String provider;
    double startLat = 0;
    double startLng = 0;
    double endLat = 0;
    double endLng = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //Setting TextViews for the coordinates
        startLatitudeField = (TextView) findViewById(R.id.txtView1);
        startLongitudeField = (TextView) findViewById(R.id.txtView2);
        endLatitudeField = (TextView) findViewById(R.id.txtView3);
        endLongitudeField = (TextView) findViewById(R.id.txtView4);

        //Button
        Button button = (Button) findViewById(R.id.cmdButton);
        button.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {

                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locationListener = new MyLocationListener ( ); 
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                if (startLat != 0) 
                {
                    locationListener.onLocationChanged(location);
                } else 
                {
                    locationListener.onLocationChanged(location);
                    Button button = (Button) findViewById(R.id.cmdButton);
                    button.setText("Get me home");
                }
            }
        }); 
    };


    private class MyLocationListener implements LocationListener
    {
        @Override
        public void onLocationChanged(Location location) 
        {
            // TODO Auto-generated method stub
            if (startLat == 0) {
                startLat = (double) (location.getLatitude());
                startLng = (double) (location.getLongitude());
                startLatitudeField.setText(String.valueOf(startLat));
                startLongitudeField.setText(String.valueOf(startLng));
            } 
            else {

                endLat = (double) (location.getLatitude());
                endLng = (double) (location.getLongitude());
                endLatitudeField.setText(String.valueOf(endLat));
                endLongitudeField.setText(String.valueOf(endLng));
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }
    }
}

这篇关于GPS应用程序 - 运行,但关闭按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆