简体   繁体   English

getAction()或Intent上的NullPointerException

[英]NullPointerException on getAction( ) or Intent

I wanted to utilize the search view widget in the action bar with the Google Places API and stumbled upon a link to a tutorial that was shared in various posts on Stack Overflow, so I decided to check it out. 我想利用Google Places API在操作栏中使用搜索视图窗口小部件,并偶然发现了在Stack Overflow上各个帖子中共享的教程的链接,因此我决定将其检出。 I have it all implemented into my existing code, but I am getting a NullPointerException and am hoping someone can help me out a bit, so I can debug the issue. 我把它全部实现到我现有的代码中,但我得到一个NullPointerException并希望有人可以帮我一点,所以我可以调试这个问题。 I've been browsing through various posts on Stack Overflow and articles via Google... 我一直在浏览Stack Overflow上的各种帖子以及谷歌的文章......

Logcat specifies that it is caused by lines 119 and 123. Logcat指定它由第119行和第123行引起。

MainActivity.java: 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);
}
...
}

AndroidManifest.xml: 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>

You are starting MainActivity from SplashActivity? 您正在从SplashActivity启动MainActivity? When something happens in the search view? 当搜索视图中出现问题时? It looks like it. 它看起来像。 When you call startActivity(intent), you need to make sure you have added an action to the intent: 当您调用startActivity(intent)时,您需要确保已向intent添加了一个操作:

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

If you, or whatever calls your MainActivity using an intent, doesn't set an action on the intent, you are going to get a null pointer when you try to do a getAction() on the calling intent in your Activity. 如果您或使用intent调用MainActivity,则不会对intent设置操作,当您尝试对Activity中的调用intent执行getAction()时,将获得空指针。

在致电您当前的活动或服务之前,先从您之前的活动发送和意图行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM