我已经制作了一个应用程序,其中我正在获取所有Facebook好友的列表,但现在我已决定在我的应用程序中添加Tab功能,但每当我运行我的应用程序时收到错误消息:
Unfortunately Facebook Friends List has stopped
Logcat说:
01-15 11:34:23.526: E/AndroidRuntime(982): FATAL EXCEPTION: main
01-15 11:34:23.526: E/AndroidRuntime(982): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chr.tatu.sample.friendslist/com.chr.tatu.sample.friendslist.TabSample}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chr.tatu.sample.friendslist/com.chr.tatu.sample.friendslist.FriendsList}: java.lang.NullPointerException
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.os.Looper.loop(Looper.java:137)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-15 11:34:23.526: E/AndroidRuntime(982): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 11:34:23.526: E/AndroidRuntime(982): at java.lang.reflect.Method.invoke(Method.java:511)
01-15 11:34:23.526: E/AndroidRuntime(982): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-15 11:34:23.526: E/AndroidRuntime(982): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-15 11:34:23.526: E/AndroidRuntime(982): at dalvik.system.NativeStart.main(Native Method)
01-15 11:34:23.526: E/AndroidRuntime(982): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chr.tatu.sample.friendslist/com.chr.tatu.sample.friendslist.FriendsList}: java.lang.NullPointerException
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.widget.TabHost.addTab(TabHost.java:240)
01-15 11:34:23.526: E/AndroidRuntime(982): at com.chr.tatu.sample.friendslist.TabSample.addTab(TabSample.java:50)
01-15 11:34:23.526: E/AndroidRuntime(982): at com.chr.tatu.sample.friendslist.TabSample.setTabs(TabSample.java:29)
01-15 11:34:23.526: E/AndroidRuntime(982): at com.chr.tatu.sample.friendslist.TabSample.onCreate(TabSample.java:25)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.Activity.performCreate(Activity.java:5104)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-15 11:34:23.526: E/AndroidRuntime(982): ... 11 more
01-15 11:34:23.526: E/AndroidRuntime(982): Caused by: java.lang.NullPointerException
01-15 11:34:23.526: E/AndroidRuntime(982): at com.chr.tatu.sample.friendslist.FriendsList.init(FriendsList.java:98)
01-15 11:34:23.526: E/AndroidRuntime(982): at com.chr.tatu.sample.friendslist.FriendsList.onCreate(FriendsList.java:57)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.Activity.performCreate(Activity.java:5104)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-15 11:34:23.526: E/AndroidRuntime(982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-15 11:34:23.526: E/AndroidRuntime(982): ... 23 more
TabSample.java:
public class TabSample extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmain);
setTabs() ;
}
private void setTabs()
{
addTab("All", R.drawable.tab_menu, FriendsList.class);
addTab("Current Month", R.drawable.tab_offers, FriendsList.class);
addTab("Current Week", R.drawable.tab_location, FriendsList.class);
addTab("Today", R.drawable.tab_reservation, FriendsList.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
FriendsList.java:
public class FriendsList extends Activity implements FacebookRequest, OnItemClickListener {
private static String LOG_TAG = "FriendsList";
private static JSONArray jsonArray;
private static ListView friendsList;
private Handler mHandler;
private Button saveGreeting;
public void onCreate(Bundle savedInstanceState) {
mHandler = new Handler();
super.onCreate(savedInstanceState);
try {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
getActionBar().hide();
} catch (Exception e) {}
setContentView(R.layout.friends_list_screen);
try {
setProgressBarIndeterminateVisibility(false);
} catch (Exception e) {}
init();
saveGreeting = (Button) findViewById(R.id.greeting);
saveGreeting.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
customGreeting(v);
}
});
}
public void customGreeting(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http://google.com/"));
startActivity(myWebLink);
}
@Override
public void onDestroy() {
super.onDestroy();
exit();
}
private void exit() {
if (friendsList != null)
friendsList.setAdapter(null);
friendsList = null;
jsonArray = null;
GetProfilePictures.clear();
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
exit();
}
private void init() {
Bundle extras = getIntent().getExtras();
String response = extras.getString("FRIENDS");
Log.d(LOG_TAG, "onCreate()" + response);
try {
jsonArray = new JSONArray(response);
} catch (JSONException e) {
FacebookUtility.displayMessageBox(this, this.getString(R.string.json_failed));
}
friendsList = (ListView) findViewById(R.id.friends_list);
friendsList.setOnItemClickListener(this);
friendsList.setAdapter(new FriendListAdapter(this));
friendsList.setTextFilterEnabled(true);
// Removing the delimiters between the items. It looks prettier
friendsList.setDivider(null);
friendsList.setDividerHeight(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(LOG_TAG, "onCreateOptionsMenu()");
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
/**
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(LOG_TAG, "onOptionsItemSelected()");
switch (item.getItemId()) {
case R.id.menu_refresh:
FacebookUtility.logout(this);
setProgressBarInDVisibility(true);
return true;
case R.id.retry_button:
GetProfilePictures.clear();
init();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
**/
public void onRequestReceived(int result, String message) {
Log.d(LOG_TAG, "onRequestReceived(" + result + ")");
if(isFinishing())
return;
// setProgressBarInDVisibility(false);
switch (result) {
case FacebookRequest.COMPLETED:
FacebookUtility.clearSession(this);
finish();
break;
case FacebookRequest.IO_EXCEPTION:
case FacebookRequest.FILE_NOT_FOUND_EXCEPTION:
case FacebookRequest.MALFORMED_URL_EXCEPTION:
case FacebookRequest.FACEBOOK_ERROR:
FacebookUtility.displayMessageBox(this, this.getString(R.string.logout_failed));
break;
}
}
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// Log.d(LOG_TAG, "onItemClick()");
try {
final long friendId;
friendId = jsonArray.getJSONObject(position).getLong("uid");
String name = jsonArray.getJSONObject(position).getString("name");
new AlertDialog.Builder(this).setTitle(R.string.post_on_wall_title)
.setMessage(String.format(getString(R.string.post_on_wall), name))
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Bundle params = new Bundle();
params.putString("to", String.valueOf(friendId));
params.putString("caption", getString(R.string.app_name));
params.putString("description", getString(R.string.app_desc));
params.putString("picture", FacebookUtility.HACK_ICON_URL);
params.putString("name", getString(R.string.app_action));
FacebookUtility.facebook.dialog(FriendsList.this, "feed", params,
(DialogListener) new PostDialogListener());
}
}).setNegativeButton(R.string.no, null).show();
} catch (JSONException e) {
showToast("Error: " + e.getMessage());
}
}
/*
* Callback after the message has been posted on friend's wall.
*/
public class PostDialogListener extends BaseDialogListener {
@Override
public void onComplete(Bundle values) {
final String postId = values.getString("post_id");
if (postId != null) {
showToast("Message posted on the wall.");
} else {
showToast("No message posted on the wall.");
}
}
}
public void showToast(final String msg) {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast toast = Toast.makeText(FriendsList.this, msg, Toast.LENGTH_LONG);
toast.show();
}
});
}
public class FriendListAdapter extends BaseAdapter implements SectionIndexer {
private LayoutInflater mInflater;
private GetProfilePictures picturesGatherer = null;
FriendsList friendsList;
private String[] sections;
Hashtable<Integer, FriendItem> listofshit = null;
public FriendListAdapter(FriendsList friendsList) {
Log.d(LOG_TAG, "FriendListAdapter()");
this.friendsList = friendsList;
sections = new String[getCount()];
listofshit = new Hashtable<Integer, FriendItem>();
for (int i = 0; i < getCount(); i++) {
try {
sections[i] = jsonArray.getJSONObject(i).getString("name").substring(0);
sections[i] = jsonArray.getJSONObject(i).getString("birthday").substring(1);
} catch (JSONException e) {
sections[i] = "";
Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
}
}
if (picturesGatherer == null) {
picturesGatherer = new GetProfilePictures();
}
picturesGatherer.setAdapterForListener(this);
mInflater = LayoutInflater.from(friendsList.getBaseContext());
}
public int getCount() {
Log.d(LOG_TAG, "getCount()");
if (jsonArray == null)
return 0;
return jsonArray.length();
}
public Object getItem(int position) {
Log.d(LOG_TAG, "getItem()");
return listofshit.get(position);
}
public long getItemId(int position) {
Log.d(LOG_TAG, "getItemId()");
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(LOG_TAG, "getView(" + position + ")");
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(position);
} catch (JSONException e) {
Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
}
FriendItem friendItem;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.single_friend, null);
friendItem = new FriendItem();
convertView.setTag(friendItem);
}
else {
friendItem = (FriendItem) convertView.getTag();
}
friendItem.friendPicture = (ImageView) convertView.findViewById(R.id.picture_square);
friendItem.friendName = (TextView) convertView.findViewById(R.id.name);
friendItem.friendDob = (TextView) convertView.findViewById(R.id.dob);
friendItem.friendLayout = (RelativeLayout) convertView.findViewById(R.id.friend_item);
try {
String uid = jsonObject.getString("uid");
String url = jsonObject.getString("pic_square");
friendItem.friendPicture.setImageBitmap(picturesGatherer.getPicture(uid, url));
} catch (JSONException e) {
Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
friendItem.friendName.setText("");
friendItem.friendDob.setText("");
}
try {
friendItem.friendName.setText(jsonObject.getString("name"));
friendItem.friendDob.setText(jsonObject.getString("birthday"));
} catch (JSONException e) {
Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
friendItem.friendName.setText("");
friendItem.friendDob.setText("");
}
listofshit.put(position, friendItem);
return convertView;
}
public int getPositionForSection(int position) {
return position;
}
public int getSectionForPosition(int position) {
return position;
}
public Object[] getSections() {
return sections;
}
}
class FriendItem {
TextView friendDob;
int id;
ImageView friendPicture;
TextView friendName;
RelativeLayout friendLayout;
}
}