我正在尝试在活动之间发送数据,这是发送代码:
public void sendToFavorites(Context context){
String vID,vThumbnail,vTitle;
Bundle extras = new Bundle();
vID = sendResult.getId().getVideoId();
vThumbnail = sendResult.getSnippet().getThumbnails().getMedium().getUrl();
vTitle = sendResult.getSnippet().getTitle();
extras.putString("id",vID);
intent = new Intent(context,Favorites.class);
intent.putExtras(extras);
startActivity(intent); <----- Application crash here.
}
这是logcat:
05-18 18:39:00.560 28528-28528/project.youtubeplayer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: project.youtubeplayer, PID: 28528
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at project.youtubeplayer.MainActivity.sendToFavorites(MainActivity.java:183)
at project.youtubeplayer.YtAdapter$1.onClick(YtAdapter.java:77)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
致电sendToFavroites:
mHolder.mVideoFavorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppUtils.showToast(result.getSnippet().getTitle() + " Was added to favorites.");
m.sendResult = result;
m.sendToFavorites(mActivity);
}
});
YtAdapter:
public class YtAdapter extends BaseAdapter {
private Activity mActivity = null;
private List<SearchResult> mVideoList = null;
private LayoutInflater mLayoutInflater = null;
MainActivity m = new MainActivity();
public YtAdapter(Activity iActivity){
mActivity = iActivity;
mLayoutInflater = LayoutInflater.from(mActivity);
}
public void setmVideoList(List<SearchResult> mVideoList){
this.mVideoList = mVideoList;
}
@Override
public int getCount() {
return (mVideoList == null)? (0):(mVideoList.size());
}
@Override
public Object getItem(int position) {
return (mVideoList != null && mVideoList.size()>position)? (mVideoList.get(position)):(null);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder mHolder;
if(convertView != null){
mHolder = (ViewHolder)convertView.getTag();
}else{
mHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.view_video_item,null);
mHolder.mVideoThumbnail = (ImageView)convertView.findViewById(R.id.video_thumbnail);
mHolder.mVideoTitle = (TextView)convertView.findViewById(R.id.video_title);
mHolder.mVideoFavorite = (ImageView)convertView.findViewById(R.id.video_favorite);
convertView.setTag(mHolder);
}
//Setting the data
final SearchResult result = mVideoList.get(position);
mHolder.mVideoTitle.setText(result.getSnippet().getTitle());
//Loading the image
Picasso.with(mActivity).load(result.getSnippet().getThumbnails().getMedium().getUrl()).into(mHolder.mVideoThumbnail);
//OnClickListeners
mHolder.mVideoFavorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppUtils.showToast(result.getSnippet().getTitle() + " Was added to favorites.");
m.sendResult = result;
m.sendToFavorites(mActivity);
}
});
mHolder.mVideoThumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppUtils.showToast("Please click on title to start video");
}
});
return convertView;
}
private class ViewHolder{
private TextView mVideoTitle = null;
private ImageView mVideoThumbnail = null;
//Testing
private ImageView mVideoFavorite = null;
}
从MainActivity调用YtAdapter:
@Override
public void completedRequest(Object... objects){
//Dismiss the dialog
if(mLoadingDialog != null && mLoadingDialog.isShowing())
mLoadingDialog.dismiss();
//Parse the response based on type of request
Integer reqCode = (Integer) objects[0];
if(reqCode == null || reqCode == 0)
throw new NullPointerException("Request Code's value is Invalid.");
switch (reqCode){
case SEARCH_VIDEO:
if(mYtadapter == null){
mYtadapter = new YtAdapter(this);
mYtadapter.setmVideoList((List<SearchResult>)objects[1]);
mVideoLsv.setAdapter(mYtadapter);
}else{
mYtadapter.setmVideoList((List<SearchResult>) objects[1]);
mYtadapter.notifyDataSetChanged();
}
break;
}
}
我试图在网上搜索该问题,但找不到任何答案.. 希望你们能帮助我。
答案 0 :(得分:1)
试一试:
在activity
中,定义context
final Context context = this;
在adapter
中使用它而不是activity
。
m.sendToFavorites(context);
因此,在adapter
中,定义Context cxt
;
public YtAdapter(Context _cxt){
....
cxt = _cxt;
}
然后使用它如下:
intent = new Intent(cxt,Favorites.class);
intent.putExtras(extras);
cxt.startActivity(intent);
当我使用adapter
时,我就这样走了。
希望它有所帮助。