這是我的代碼,先點擊一個圖片按鈕,然後跳轉到授權界面,當授權完成的時候,再把值返回在這個類裡面,但是運行的時候一直報這句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)
有返回值說明程序沒錯,是你這邊獲取登錄的代碼報錯。userInfo.getUserName().toString()試試