私はAndroidアプリを作成しています。私が使用している2つのボタン( "btnGPSShowLocation"と "button")はnullです。私はそれらを両方ともインスタンス化しているので、なぜ彼らがnullであるのかわかりません。ここに私のコードは次のとおりです。これらの2つのオブジェクトはなぜ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)
ポストstracktraceについてはこちらをご覧ください! –
ヌルオブジェクトエラー –
を表示するlogcatを投稿すると、ボタンを含む 'activity_main.xml'が確認できますか? –