我試圖使用位於LocationActivity.java中的checkGoogleplay()onCreate MainActivity.java,但應用程序在運行時崩潰。 如果我合併兩個java文件,它的工作原理,所以它可能涉及到進口問題?任何人的幫助,感謝從OnCreate調用另一個java方法時出現NullPointerException
這是我LocationActivity
package com.example.location_calculator;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
public class LocationActivity extends Activity implements LocationListener,GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener
{
private String TAG = this.getClass().getSimpleName();
LocationRequest mLocationRequest;
LocationClient mLocationClient;
Location mCurrentLocation;
TextView txtinstant;
int resultCode;
public void checkGoogleplay()
{
resultCode =GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
if(ConnectionResult.SUCCESS == resultCode){
Log.d("Location Updates",
"Google Play services is available.");
mLocationClient = new LocationClient(this, this, this);
mLocationClient.connect();
}
else{
Toast.makeText(this, "Google Play Service Error " + resultCode,Toast.LENGTH_LONG).show();
}
}
這裏是我的MainActivity
package com.example.location_calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView txtlocation;
TextView txtstatus;
TextView txtinstant;
Button btn_loc;
Button btn_ser;
Button bnt_aploc;
LocationActivity mlocal;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlocal =new LocationActivity();
mlocal.checkGoogleplay();
}
這裏是目錄下載
08-23 15:19:12.576: W/dalvikvm(18236): threadid=1: thread exiting with uncaught exception (group=0x4190a700)
08-23 15:19:12.576: E/AndroidRuntime(18236): FATAL EXCEPTION: main
08-23 15:19:12.576: E/AndroidRuntime(18236): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.location_calculator/com.example.location_calculator.MainActivity}: java.lang.NullPointerException
08-23 15:19:12.576: E/AndroidRuntime(18236): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-23 15:19:12.576: E/AndroidRuntime(18236): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-23 15:19:12.576: E/AndroidRuntime(18236): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-23 15:19:12.576: E/AndroidRuntime(18236): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-23 15:19:12.576: E/AndroidRuntime(18236): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 15:19:12.576: E/AndroidRuntime(18236): at android.os.Looper.loop(Looper.java:137)
08-23 15:19:12.576: E/AndroidRuntime(18236): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-23 15:19:12.576: E/AndroidRuntime(18236): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 15:19:12.576: E/AndroidRuntime(18236): at java.lang.reflect.Method.invoke(Method.java:525)
08-23 15:19:12.576: E/AndroidRuntime(18236): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-23 15:19:12.576: E/AndroidRuntime(18236): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-23 15:19:12.576: E/AndroidRuntime(18236): at dalvik.system.NativeStart.main(Native Method)
08-23 15:19:12.576: E/AndroidRuntime(18236): Caused by: java.lang.NullPointerException
08-23 15:19:12.576: E/AndroidRuntime(18236): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
08-23 15:19:12.576: E/AndroidRuntime(18236): at com.example.location_calculator.LocationActivity.checkGoogleplay(LocationActivity.java:30)
08-23 15:19:12.576: E/AndroidRuntime(18236): at com.example.location_calculator.MainActivity.onCreate(MainActivity.java:35)
08-23 15:19:12.576: E/AndroidRuntime(18236): at android.app.Activity.performCreate(Activity.java:5133)
08-23 15:19:12.576: E/AndroidRuntime(18236): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-23 15:19:12.576: E/AndroidRuntime(18236): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-23 15:19:12.576: E/AndroidRuntime(18236): ... 11 more
只需在super.oncreate()方法 –
之前調用此方法,您是否在ur文件清單文件中添加了此方法? – Piyush
什麼是'getBaseContext()'?它可以爲空嗎?你爲什麼不使用'this'而不是它呢? –