我正在尝试在onCreate MainActivity.java 中使用 LocationActivity.java 中的 checkGoogleplay(),但应用程序在运行时崩溃了。如果我合并两个 java 文件,它可以工作,所以它可能与导入问题有关?有人帮忙,谢谢
这是我的 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