Java getAction()或Intent上的NullPointerException

Java getAction()或Intent上的NullPointerException,java,android,nullpointerexception,Java,Android,Nullpointerexception,我想利用GooglePlacesAPI操作栏中的搜索视图小部件,偶然发现了一个教程链接,该教程在关于堆栈溢出的各种帖子中共享,所以我决定去看看。我已经在我现有的代码中实现了这一切,但是我得到了一个NullPointerException,我希望有人能帮我一点忙,这样我就可以调试这个问题了。我一直在通过谷歌浏览关于堆栈溢出的各种帖子和文章 Logcat指定它是由第119行和第123行引起的 MainActivity.java: public class MainActivity extends F

我想利用GooglePlacesAPI操作栏中的搜索视图小部件,偶然发现了一个教程链接,该教程在关于堆栈溢出的各种帖子中共享,所以我决定去看看。我已经在我现有的代码中实现了这一切,但是我得到了一个NullPointerException,我希望有人能帮我一点忙,这样我就可以调试这个问题了。我一直在通过谷歌浏览关于堆栈溢出的各种帖子和文章

Logcat指定它是由第119行和第123行引起的

MainActivity.java:

public class MainActivity extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener,
OnMapLongClickListener,
LoaderCallbacks<Cursor> {

//Global Constants
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

// Milliseconds per second
private static final int MILLISECONDS_PER_SECOND = 1000;
// Update frequency in seconds
public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
// Update frequency in milliseconds
private static final long UPDATE_INTERVAL =
        MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;
// The fastest update frequency, in seconds
private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
// A fast frequency ceiling in milliseconds
private static final long FASTEST_INTERVAL =
        MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;


//Global Variables
GoogleMap map;
Marker marker;
LocationRequest requestLocation;
LocationClient locationClient;
LocationManager locationManager;
boolean updatesRequested;
Geofence geoFence;
Vibrator vibStatus;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the LocationRequest object
    requestLocation = LocationRequest.create();
    // Use high accuracy
    requestLocation.setPriority(
            LocationRequest.PRIORITY_HIGH_ACCURACY);
    // Set the update interval to 5 seconds
    requestLocation.setInterval(UPDATE_INTERVAL);
    // Set the fastest update interval to 1 second
    requestLocation.setFastestInterval(FASTEST_INTERVAL);


    SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    map = fragment.getMap();

    //Enables "My Location" button on map fragment
    map.getUiSettings().setMyLocationButtonEnabled(true);

    //Create a new location client, using the enclosing class to handle callbacks.
    locationClient = new LocationClient(this, this, this);
    // Start with updates turned off
    updatesRequested = false;

    //
    map.setOnMapLongClickListener(this);

    // Look up the AdView as a resource and load a request.
    AdView adView = (AdView)this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    handleIntent(getIntent()); //This is line 119 in my code
  }

private void handleIntent(Intent intent){
    if(intent.getAction().equals(Intent.ACTION_SEARCH)){ //This is line 123 in my code
        doSearch(intent.getStringExtra(SearchManager.QUERY));
    }else if(intent.getAction().equals(Intent.ACTION_VIEW)){
        getPlace(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
    }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    handleIntent(intent);
}
...
}
public类MainActivity扩展了FragmentActivity实现
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener,
在MaplongClickListener上,
装载机回扣{
//全局常数
专用最终静态int连接\故障\解决\请求=9000;
//毫秒每秒
私有静态最终整数毫秒/秒=1000;
//以秒为单位更新频率
公共静态最终整型更新间隔(以秒为单位)=5;
//更新频率(毫秒)
私有静态最终长更新间隔=
毫秒/秒*更新间隔秒;
//最快的更新频率,以秒为单位
私有静态最终整数最快间隔(秒)=1;
//以毫秒为单位的快速频率上限
专用静态最终长最快\u间隔=
毫秒/秒*最快的时间间隔(以秒为单位);
//全局变量
谷歌地图;
标记;
位置请求位置;
地点客户地点客户;
地点经理地点经理;
布尔更新要求;
土工栅栏;
可控震源振动状态;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建LocationRequest对象
requestLocation=LocationRequest.create();
//使用高精度
requestLocation.setPriority(
定位请求。优先级(高精度);
//将更新间隔设置为5秒
requestLocation.setInterval(更新间隔);
//将最快更新间隔设置为1秒
requestLocation.SetFastTestInterval(最快的\u间隔);
SupportMapFragment fragment=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
map=fragment.getMap();
//在地图片段上启用“我的位置”按钮
map.getUiSettings().setMyLocationButtonEnabled(true);
//创建一个新的location客户端,使用封闭类来处理回调。
locationClient=新locationClient(这个,这个,这个);
//从关闭更新开始
updateRequested=false;
//
map.setOnMapLongClickListener(此);
//将AdView作为资源查找并加载请求。
AdView AdView=(AdView)this.findviewbyd(R.id.AdView);
AdRequest AdRequest=新建AdRequest.Builder().build();
adView.loadAd(adRequest);
handleIntent(getIntent());//这是代码中的第119行
}
私人无效手册内容(意图){
如果(intent.getAction().equals(intent.ACTION_SEARCH)){//这是我代码中的第123行
doSearch(intent.getStringExtra(SearchManager.QUERY));
}else if(intent.getAction().equals(intent.ACTION_视图)){
getPlace(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
}
}
@凌驾
受保护的void onNewIntent(意图){
super.onNewIntent(意向);
设定意图(intent);
手册内容(意图);
}
...
}
AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com...." //Removed while posting on forum
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="19" />

<!-- Protect the map component of the application using application signature -->
<permission
    android:name="com.....permission.MAPS_RECEIVE" //Removed while posting on forum
    android:protectionLevel="signature" />

<!-- Allows to receive map -->
<uses-permission android:name="com.....permission.MAPS_RECEIVE" /> //Removed while posting on forum

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
    <activity
        android:name="com.....SplashActivity" //Removed while posting on forum
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.....MainActivity" //Removed while posting on forum
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <!-- Points to searchable activity -->
        <meta-data android:name="android.app.default_searchable"
            android:value="com.....MainActivity" /> //Removed while posting on forum

        <!-- Points to searchable meta data -->
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    </activity>

    <activity
        android:name="com.....PreferencesActivity" //Removed while posting on forum
        android:label="@string/title_activity_settings"
        android:parentActivityName="com.....MainActivity" //Removed while posting on forum
        android:screenOrientation="portrait" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.....MainActivity" /> //Removed while posting on forum
    </activity>
    <activity
        android:name="com.....AlarmActivity" //Removed while posting on forum
        android:label="@string/title_activity_alarm" >
    </activity>
    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    <activity
        android:name="com.....PlaceJSONParser" //Removed while posting on forum
        android:label="@string/title_activity_place_jsonparser" >
    </activity>
    <activity
        android:name="com.....PlaceDetailsJSONParser" //Removed while posting on forum
        android:label="@string/title_activity_place_details_jsonparser" >
    </activity>
    <activity
        android:name="com.....PlaceProvider" //Removed while posting on forum
        android:label="@string/title_activity_place_provider" >
    </activity>

     <provider
        android:name=".PlaceProvider"
        android:authorities="com.....PlaceProvider" //Removed while posting on forum
        android:exported="false" />

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="MY-KEY-HERE" /> //Removed while posting on forum

</application>

</manifest>

//在论坛上发布时删除
//在论坛上发布时删除
//在论坛上发布时删除
//在论坛上发布时删除

您正在从SplashActivity启动MainActivity?当搜索视图中发生某些事情时?看起来是这样。调用startActivity(意图)时,需要确保已将操作添加到意图中:

Intent mainActivityIntent = new Intent(context);
mainActivityIntent.setAction(Intent.ACTION_SEARCH);
startActivity(mainActivityIntent);

如果您或使用意图调用MainActivity的任何人未对意图设置操作,则当您尝试在活动中对调用意图执行getAction()时,将得到一个空指针。

在调用当前活动或服务之前,从上一个活动发送和意图操作

第119行和第123行是什么?若你们看的话,我已经用注释标记了这两行。我想我们在这里看到了一些东西……我看了一下我的SplashActivity,就意图而言:
finish();意图i=新意图();i、 setClassName(“com.j5tech.geolarm”、“com.j5tech.geolarm.MainActivity”);星触觉(i)
I将action语句添加到混合中,
I.setAction(Intent.action\u SEARCH),现在我似乎已经越过了那个错误,进入了一个新的错误,我不确定它是否相关。