我正在尝试使用位于 onCreate MainActivity.java中的LocationActivity.java中的checkGoogleplay(),但app在运行时崩溃了。 如果我合并两个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();
}
这是Catlog
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
答案 0 :(得分:2)
你这样做
mlocal =new LocationActivity(); // which is wrong
活动由startActivity
如果需要从LoacationActivity向MainActivity传递值,请使用Intent(考虑到LocationActivity是一个活动类)。
此外,您还有public class LocationActivity extends Activity
但我在onCreate
LocationActivity
方法
同时检查此
http://developer.android.com/training/location/retrieve-current.html
查看检查Google Play服务
下的主题答案 1 :(得分:1)
您不能只使用以下方法创建活动对象:
MyActivity活动=新MyActivity();
就像使用普通的Java类一样。 Android中的所有活动都必须通过活动生命周期,以便它们附加有有效的上下文。通过将Activity视为普通的Java类,您最终会得到一个空上下文。由于Activity中的大多数方法都在其Context上调用,因此您将获得一个空指针异常,这就是您的应用程序崩溃的原因。
相反,将需要从其他类调用的所有此类方法移动到Utility类中,该类在其构造函数中接受有效上下文,然后在方法中使用该上下文来完成工作。
答案 2 :(得分:1)
第一件事......我可以知道,LocationActivity.java活动中的onCreate()方法在哪里。
和'mlocal = new LocationActivity();'您需要启动该活动是错误的,否则不要在LocationActivity文件中扩展Activity。
您是否在清单中声明了LocationActivity?
LocationRequest mLocationRequest; LocationClient mLocationClient;
你可以探索上面两个类,可能会在这些类中变得无效。
答案 3 :(得分:1)
在位置活动中添加Oncreate方法
在清单
中添加添加位置活动答案 4 :(得分:0)
在AndroidManifest.xml文件中添加以下代码:
<activity
android:name=".LocationActivity" />
在MainActivity下面...
答案 5 :(得分:0)
只需将Context
对象传递给GooglePlayServicesUtil
public void checkGoogleplay()
{
resultCode =GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
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();
}
方法在Activity和Activity中与Simple Java文件不同,因此,您需要启动它而不是为它创建对象:
Intent i= new Intent(this, LocationActivity.class);
startActivity(i);
答案 6 :(得分:0)
更新您的方法
public void checkGoogleplay(Context mContext)
{
resultCode =GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
if(ConnectionResult.SUCCESS == resultCode){
Log.d("Location Updates",
"Google Play services is available.");
mLocationClient = new LocationClient(mContext, this, this);
mLocationClient.connect();
}
else{
Toast.makeText(this, "Google Play Service Error " + resultCode,Toast.LENGTH_LONG).show();
}
}
答案 7 :(得分:0)
使LocationActivity成为普通的Java类,并将MainActivity上下文传递给构造函数中的该类。
仅在您要显示视图的地方扩展活动。我认为没有必要在LocationActivity中扩展Activity