当我尝试将tab添加到我的操作栏时,我得到了这个nullpointexception。
以下是相关代码。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayShowTitleEnabled(true);
frame = (FrameLayout) findViewById(R.id.flFrame);
tab1 = actionbar.newTab().setText("Events")
.setTabListener(MainActivity.this);
tab2 = actionbar.newTab().setText("Conversation")
.setTabListener(MainActivity.this);
actionbar.addTab(tab1);
actionbar.addTab(tab2);
actionbar.selectTab(tab1);
if (pref.getString("ISLOGIN", null) != null) {
if (pref.getString("ISLOGIN", null).equals("true")) {
fragment = new ProfileFragment();
} else {
fragment = new LoginFragment();
}
} else {
fragment = new LoginFragment();
}
openFragment(fragment);
}
执行此行代码actionbar.addTab(tab1);
时会发生错误,表示操作栏为空。怎么会是空的?什么可能是解决这个问题?
这是logcat输出
08-04 04:58:14.650: E/AndroidRuntime(5552): FATAL EXCEPTION: main
08-04 04:58:14.650: E/AndroidRuntime(5552): java.lang.RuntimeException: Unable to start activity ComponentInfo{philsutech.mobile.app/philsutech.mobile.app.MainActivity}: java.lang.NullPointerException
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.os.Looper.loop(Looper.java:137)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-04 04:58:14.650: E/AndroidRuntime(5552): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 04:58:14.650: E/AndroidRuntime(5552): at java.lang.reflect.Method.invoke(Method.java:511)
08-04 04:58:14.650: E/AndroidRuntime(5552): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-04 04:58:14.650: E/AndroidRuntime(5552): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-04 04:58:14.650: E/AndroidRuntime(5552): at dalvik.system.NativeStart.main(Native Method)
08-04 04:58:14.650: E/AndroidRuntime(5552): Caused by: java.lang.NullPointerException
08-04 04:58:14.650: E/AndroidRuntime(5552): at philsutech.mobile.app.MainActivity.onTabSelected(MainActivity.java:452)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.support.v7.app.ActionBarImplICS$TabWrapper.onTabSelected(ActionBarImplICS.java:558)
08-04 04:58:14.650: E/AndroidRuntime(5552): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
08-04 04:58:14.650: E/AndroidRuntime(5552): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:499)
08-04 04:58:14.650: E/AndroidRuntime(5552): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:485)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.support.v7.app.ActionBarImplICS.addTab(ActionBarImplICS.java:247)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.support.v7.app.ActionBarImplJB.addTab(ActionBarImplJB.java:20)
08-04 04:58:14.650: E/AndroidRuntime(5552): at philsutech.mobile.app.MainActivity.onCreate(MainActivity.java:91)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.app.Activity.performCreate(Activity.java:5008)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-04 04:58:14.650: E/AndroidRuntime(5552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
答案 0 :(得分:0)
修好了。 Changdeo说,错误是onTabSelected()。它是与actionBar无关的对象。在我初始化之前,我打电话给那个对象做了一些事情。
似乎初始化选项卡后,它会自动调用onTabSelected方法。