q12q1ty
Even_P
采纳率57.1%
2015-10-30 01:56

请大神指教!shareSDK第三方登录,返回了用户登录信息,但是一直报空指针异常,可是有返回值

已采纳

这是我的代码,先点击一个图片按钮,然后跳转到授权界面,当授权完成的时候,再把值返回在这个类里面,但是运行的时候一直报这句left_menu_fragment_textview.setText(userInfo.getUserName());是空指针异常,这是怎么回事

public class LeftMenuFragment extends BaseFragment {
private ImageButton left_menu_fragment_imagebutton;
private ListView left_menu_fragment_listview;
private String[] itemname = new String[] { "设置文字大小", "收藏", "检查更新" };
/************************************ 后面增加的方法 **********************************************/
private OnLoginListener signupListener;
private Platform platform;

private String picturePath;
private UserInfo userInfo = new UserInfo();
/** 加载用户icon */
private static final int LOAD_USER_ICON = 2;
/** 图片名字 */
private static final String PICTURE_NAME = "userIcon.jpg";
private TextView left_menu_fragment_textview;

/**********************************************************************************/

@Override
public View initview() {
    View view = View.inflate(mActivity, R.layout.left_menu_fragment, null);

    left_menu_fragment_imagebutton = (ImageButton) view
            .findViewById(R.id.left_menu_fragment_imagebutton);
    left_menu_fragment_listview = (ListView) view
            .findViewById(R.id.left_menu_fragment_listview);
    left_menu_fragment_textview = (TextView) view
            .findViewById(R.id.left_menu_fragment_textview);
    left_menu_fragment_listview.setAdapter(new myListAdapter());
    left_menu_fragment_imagebutton
            .setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.i("pan", "已经点击跳转界面");
                    Intent intent = new Intent();
                    intent.setClass(mActivity,
                            AuthorizationLoginActivity.class);
                    mActivity.startActivity(intent);

                }
            });
    return view;
}

/**
 * 初始化数据
 */
@Override
public void initData() {

    if (platform != null) {
        userInfo.setUserIcon(platform.getDb().getUserIcon());
        userInfo.setUserName(platform.getDb().getUserName());

        Log.i("pan", "leftMenuFragment中的username" + userInfo.getUserName());
        Log.i("pan", "leftMenuFragment中的icon" + userInfo.getUserIcon());

        if (userInfo.getUserName() != null) {
            Log.i("pan", "leftMenuFragment中的username222222" + userInfo.getUserName());

            left_menu_fragment_textview.setText(userInfo.getUserName());
        }
    }
    if (!TextUtils.isEmpty(userInfo.getUserIcon())) {
        Log.i("pan", "执行进入了获取icon方法吗");
        loadIcon();
    }
    // 初始化照片保存地址
    if (Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED)) {
        String thumPicture = Environment.getExternalStorageDirectory()
                .getAbsolutePath()
                + "/"
                + mActivity.getPackageName()
                + "/download";
        File pictureParent = new File(thumPicture);
        File pictureFile = new File(pictureParent, PICTURE_NAME);

        if (!pictureParent.exists()) {
            pictureParent.mkdirs();
        }
        try {
            if (!pictureFile.exists()) {
                pictureFile.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        picturePath = pictureFile.getAbsolutePath();
        Log.e("picturePath ==>>", picturePath);
    } else {
        Log.e("change user icon ==>>", "there is not sdcard!");
    }

    // Bundle bundle = mActivity.getIntent().getExtras();
    // if (bundle != null) {
    //
    // Log.i("pan", "LeftMenuFragment中的res值:" + bundle.get("myMap"));
    // Map<String, Object> hashmap = (Map<String, Object>)
    // bundle.getSerializable("myMap");
    // String name = (String) hashmap.get("nickname");
    // Log.i("pan", "LeftMenuFragment中的res值:" + hashmap);
    // Log.i("pan", "LeftMenuFragment中的name值:" + name);
    // }
}

private Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {

        case LOAD_USER_ICON:
            left_menu_fragment_imagebutton.setImageURI(Uri
                    .parse(picturePath));
            break;
        default:
            break;
        }
    };
};

/**
 * 加载头像
 */
public void loadIcon() {
    final String imageUrl = platform.getDb().getUserIcon();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL picUrl = new URL(imageUrl);
                Bitmap userIcon = BitmapFactory.decodeStream(picUrl
                        .openStream());
                FileOutputStream b = null;
                try {
                    b = new FileOutputStream(picturePath);
                    userIcon.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        b.flush();
                        b.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                userInfo.setUserIcon(picturePath);

                Message msg = new Message();
                msg.what = LOAD_USER_ICON;
                // UIHandler.sendMessage(msg);

                handler.sendMessage(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}

public boolean handleMessage(Message msg) {
    switch (msg.what) {

    case LOAD_USER_ICON:
        left_menu_fragment_imagebutton.setImageURI(Uri.parse(picturePath));
        break;
    default:
        break;
    }
    return false;
}

/**
 * listview的适配器
 *
 * @author Administrator
 *
 */
class myListAdapter extends BaseAdapter {

    private TextView text;

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemname.length;
    }

    @Override
    public Object getItem(int position) {
        return itemname[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mActivity,
                    R.layout.left_menu_fragment_listview, null);
        }
        text = (TextView) convertView
                .findViewById(R.id.left_menu_fragment_listview_textview);
        text.setText(itemname[position]);
        return convertView;
    }

}

/****************************************************************************************/

/** 设置授权回调,用于判断是否进入注册 */
public void setOnLoginListener(OnLoginListener l) {
    this.signupListener = l;
}

public void setPlatform(String platName) {
    Log.i("pan", "platName的值为:" + platName);
    platform = ShareSDK.getPlatform(platName);
    initData();

}

这下面是打印的值:

10-30 09:11:52.480: I/pan(8062): leftMenuFragment中的username额路的快乐
10-30 09:11:52.480: I/pan(8062): leftMenuFragment中的iconhttp://q.qlogo.cn/qqapp/100371282/C9F7933B7F1457206982CCF2EC3548F9/40
10-30 09:11:52.480: I/pan(8062): leftMenuFragment中的username222222额路的快乐
10-30 09:11:52.480: I/pan(8062): 执行进入了获取icon方法吗

这下面是错误的提示:

10-30 09:11:52.490: E/AndroidRuntime(8062): java.lang.NullPointerException
10-30 09:11:52.490: E/AndroidRuntime(8062): at com.pan.foucstoday.fragment.LeftMenuFragment.initData(LeftMenuFragment.java:121)
10-30 09:11:52.490: E/AndroidRuntime(8062): at com.pan.foucstoday.fragment.LeftMenuFragment.setPlatform(LeftMenuFragment.java:270)
10-30 09:11:52.490: E/AndroidRuntime(8062): at com.pan.foucstoday.AuthorizationLoginActivity.handleMessage(AuthorizationLoginActivity.java:201)
10-30 09:11:52.490: E/AndroidRuntime(8062): at com.mob.tools.utils.UIHandler.handleMessage(Unknown Source)
10-30 09:11:52.490: E/AndroidRuntime(8062): at com.mob.tools.utils.UIHandler.access$000(Unknown Source)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • FJeKin FJekin 6年前

    有返回值说明程序没错,是你这边获取登录的代码报错。userInfo.getUserName().toString()试试

    点赞 评论 复制链接分享
  • crazy1235 月盡天明 6年前

    你看看userinfo是不是空,,然后看看userinfo. getUserName()是不是空。

    点赞 评论 复制链接分享
  • crazy1235 月盡天明 6年前

    空指针你看看哪里的问题啊、、
    可能不是sharesdk那一块的问题了、

    点赞 评论 复制链接分享
  • q12q1ty Even_P 6年前

    还是报这个错呢,这里他说UIHandler没有资源,但是我没有用UIHandle呢,我是自己定义的一个Handle图片说明

    10-30 10:31:37.370: E/AndroidRuntime(11735): java.lang.NullPointerException
    10-30 10:31:37.370: E/AndroidRuntime(11735): at com.pan.foucstoday.fragment.LeftMenuFragment.initData(LeftMenuFragment.java:101)
    10-30 10:31:37.370: E/AndroidRuntime(11735): at com.pan.foucstoday.fragment.LeftMenuFragment.setPlatform(LeftMenuFragment.java:263)
    10-30 10:31:37.370: E/AndroidRuntime(11735): at com.pan.foucstoday.AuthorizationLoginActivity.handleMessage(AuthorizationLoginActivity.java:201)
    10-30 10:31:37.370: E/AndroidRuntime(11735): at com.mob.tools.utils.UIHandler.handleMessage(Unknown Source)
    10-30 10:31:37.370: E/AndroidRuntime(11735): at com.mob.tools.utils.UIHandler.access$000(Unknown Source)
    10-30 10:31:37.370: E/AndroidRuntime(11735): at com.mob.tools.utils.UIHandler$1.handleMessage(Unknown Source)

    点赞 评论 复制链接分享