gpt4 book ai didi

java - 从 OnCreate 调用另一个 java 方法时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 07:55:00 26 4
gpt4 key购买 nike

我正在尝试在 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

最佳答案

你在做这个

   mlocal =new LocationActivity(); // which is wrong

Activity 由 startActivity 启动

如果您需要将值从 LoacationActivity 传递给 MainActivity,请使用 intents(考虑到 LocationActivity 是一个 Activity 类)。

还有这个public class LocationActivity extends Activity

但是我在 LocationActivity 中没有看到任何 onCreate 方法

也检查这个

http://developer.android.com/training/location/retrieve-current.html

查看检查 Google Play 服务下的主题

关于java - 从 OnCreate 调用另一个 java 方法时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18395536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com