簡體   English   中英

為什么這兩個對象為null?

[英]Why are these two objects null?

我正在編寫一個Android應用程序,正在使用的兩個按鈕(“ btnGPSShowLocation”和“ button”)為null。 我都實例化了它們,所以我不知道為什么它們為空。 這是我的代碼:

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

這是可能相關的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>

編輯:這是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)

刪除tools:showIn="@layout/activity_gps"

此屬性設置在另一個布局編輯的布局的根元素上。 這使您可以指向包含此布局的布局之一,並且在設計時,將使用周圍的外部布局來渲染此包含的布局。

請參閱此處以供參考

暫無
暫無

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

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