简体   繁体   English

为什么这两个对象为null?

[英]Why are these two objects null?

I'm writing an Android app and two buttons ("btnGPSShowLocation" and "button") that I am using are null. 我正在编写一个Android应用程序,正在使用的两个按钮(“ btnGPSShowLocation”和“ button”)为null。 I've instantiated them both so I don't know why they are null. 我都实例化了它们,所以我不知道为什么它们为空。 Here is my code: 这是我的代码:

public class AndroidLocationActivity extends Activity {
Button btnGPSShowLocation;
Button button;


AppLocationService appLocationService;
private final static int INTERVAL = 1000 * 60 * 2; //2 minutes
Handler mHandler = new Handler();


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    appLocationService = new AppLocationService(
            AndroidLocationActivity.this);

    btnGPSShowLocation = (Button) findViewById(R.id.btnGPSShowLocation);

    btnGPSShowLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            Location gpsLocation = appLocationService
                    .getLocation(LocationManager.GPS_PROVIDER);

            if (gpsLocation != null) {
                double latitude = gpsLocation.getLatitude();
                double longitude = gpsLocation.getLongitude();
                Toast.makeText(
                        getApplicationContext(),
                        "Mobile Location (GPS): \nLatitude: " + latitude
                                + "\nLongitude: " + longitude,
                        Toast.LENGTH_LONG).show();
            } else {
                showSettingsAlert("GPS");
            }

        }
    });

    button = (Button) findViewById(R.id.button12);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent("locationActivity"));
        }
    });
} 

And here is the XML that may be relevant: 这是可能相关的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="it226.myapplicationit226androidapp.AndroidLocationActivity"
tools:showIn="@layout/activity_gps">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Location\n(GPS)"
    android:id="@+id/btnGPSShowLocation"
    android:layout_alignParentTop="true"
    android:layout_marginTop="83dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/nextgps"
    android:id="@+id/button12"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="106dp" />
</RelativeLayout>

Edit: here is the logcat 编辑:这是logcat

> 04-27 23:34:30.525 18310-18310/it226.myapplicationit226androidapp
> E/AndroidRuntime: FATAL EXCEPTION: main
>                                                                                     Process: it226.myapplicationit226androidapp, PID: 18310
>                                                                                     java.lang.RuntimeException: Unable to start activity
> ComponentInfo{it226.myapplicationit226androidapp/it226.myapplicationit226androidapp.AndroidLocationActivity}:
> java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
> on a null object reference
>                                                                                         at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
>                                                                                         at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
>                                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)
>                                                                                         at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
>                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
>                                                                                         at android.os.Looper.loop(Looper.java:148)
>                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
>                                                                                         at java.lang.reflect.Method.invoke(Native Method)
>                                                                                         at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
>                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
>                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual
> method 'void
> android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
> on a null object reference
>                                                                                         at
> it226.myapplicationit226androidapp.AndroidLocationActivity.onCreate(AndroidLocationActivity.java:39)
>                                                                                         at android.app.Activity.performCreate(Activity.java:6237)
>                                                                                         at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
>                                                                                         at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
>                                                                                         at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
>                                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
>                                                                                         at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
>                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
>                                                                                         at android.os.Looper.loop(Looper.java:148) 
>                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
>  
                                                                                   at java.lang.reflect.Method.invoke(Native Method)

Remove tools:showIn="@layout/activity_gps" 删除tools:showIn="@layout/activity_gps"

This attribute set on the root element of a layout that 'ed by another layout. 此属性设置在另一个布局编辑的布局的根元素上。 This allows you to point to one of the layouts which includes this layout, and at designtime this included layout will be rendered with the outer layout surrounding it. 这使您可以指向包含此布局的布局之一,并且在设计时,将使用周围的外部布局来渲染此包含的布局。

See here for reference 请参阅此处以供参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM