1

我有这行代码,我在这里得到一个 NullPointerException

第 219 行 Search.java

Intent intent = new Intent((Context) actionBar, SearchResultsMap.class)

例外

04-25 17:30:00.485: W/System.err(6518): java.lang.NullPointerException
04-25 17:30:00.501: W/System.err(6518):     at android.content.ComponentName.<init>(ComponentName.java:75)
04-25 17:30:00.501: W/System.err(6518):     at android.content.Intent.<init>(Intent.java:2823)
04-25 17:30:00.501: W/System.err(6518):     at com.itaxeeta.server.Search.onPostExecute(Search.java:219)

AndroidManifest

<activity
            android:name="com.itaxeeta.SearchResultsMap"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Light.NoTitleBar" />

似乎 SearchResultsMap.class 即将作为 Null 出现,但它怎么可能,它是一个 Activity 并且它就在那里。它经常随机发生。我错过了什么?

编辑:为了清楚起见,添加了更多代码。

内部活动

public class HomeScreen extends MapActivity implements IActionBar
   ....
   onClick() {
   ....
   new Search(this, source, destination).execute();    
   ...
   }
}

搜索类

 public Search(IActionBar actionBar, GeoPoint source, GeoPoint destination) {
    super(actionBar);
    this.source = source;
    this.destination = destination;
}

public interface IActionBar {
    public void setBusyVisibility(int visibility) ;
    public void setSubscribedToCount() ;
    public void setCurrentAddress(String address) ;
    public void setSubscribedToDetails(CustomJourneyUserInformation[] result);
    public Context getContext() ;
}
4

4 回答 4

2

利用

Intent intent = new Intent(getApplicationContext(), SearchResultsMap.class)

或者

Intent intent = new Intent(Your_ClassName.this, SearchResultsMap.class)

代替

Intent intent = new Intent((Context) actionBar, SearchResultsMap.class)
于 2013-04-25T12:27:35.990 回答
0

ActionBar cannot be cast to Context, more than that, seems it's null at the time you instantiate the Intent. If this code is executed inside an Activity, just pass this as the first parameter.

于 2013-04-25T12:16:18.997 回答
0

如果你想在 AsyncTask 中使用 Intent 然后使用这个代码,

public class Search extends AsyncTask<Main, Void, Main> {  
      @Override
        protected Main doInBackground(Main... params) { 
             return params[0]; 
       }
     protected void onPostExecute(Main params) { 
    // TODO Auto-generated method stub
         Intent intent = new Intent(params,SearchResultsMap.class); 
         startActivity(intent);
    }
    }
于 2013-04-25T14:48:43.000 回答
0

我也有同样的例外。就我而言,这是因为当我运行以下代码时: Intent intent = new Intent(activity, MyActivity.class) 活动已经完成。那么它是空的。

希望帮助别人。

于 2016-04-28T06:07:24.593 回答