我正在创建一个自定义ListView,其中我的ListView xml是keywordList。我还使用教程http://mtmurdockblog.com/2011/10/17/navigation-bar-for-android/以编程方式创建自定义导航栏。因为有两个 setContentView()
一个用于 ListView xml,另一个用于导航栏。我遇到了异常,你们可以帮我解决下面的代码吗?
public class KeywordListView extends Activity{
public LinkedList<String> mListItems;
KeyWordAdapter keywordAdapter;
ProgressDialog progressDialog=null;
Runnable updateRunnable;
public static String email;
Button btnSettings;
Button btnAddKeyWord;
static ListView lv;
public final Handler myHandler = new Handler();
static ArrayAdapter<String> arrayAdapter;
public static List<KeyWordDetails>responseArray;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.keywordlist);
LinearLayout ll = new LinearLayout(this);
NavigationBar nb = new NavigationBar(this);
nb.setLeftBarButton("Back");
nb.setRightBarButton("Menu");
nb.setBarTitle("The Bar Title");
NavigationBar.NavigationBarListener nbl = new NavigationBar.NavigationBarListener() {
@Override
public void OnNavigationButtonClick(int which) {
// TODO Auto-generated method stub
}
};
nb.setNavigationBarListener(nbl);
ll.addView(nb);
setContentView(ll);
错误日志:-
12-04 13:17:49.780: E/AndroidRuntime(1533): FATAL EXCEPTION: main
12-04 13:17:49.780: E/AndroidRuntime(1533): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.example.shortcode/com.example.shortcode.KeywordListView}: java.lang.NullPointerException
12-04 13:17:49.780: E/AndroidRuntime(1533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-04 13:17:49.780: E/AndroidRuntime(1533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-04 13:17:49.780: E/AndroidRuntime(1533): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-04 13:17:49.780: E/AndroidRuntime(1533): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-04 13:17:49.780: E/AndroidRuntime(1533): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 13:17:49.780: E/AndroidRuntime(1533): at android.os.Looper.loop(Looper.java:123)
12-04 13:17:49.780: E/AndroidRuntime(1533): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-04 13:17:49.780: E/AndroidRuntime(1533): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 13:17:49.780: E/AndroidRuntime(1533): at java.lang.reflect.Method.invoke(Method.java:507)
12-04 13:17:49.780: E/AndroidRuntime(1533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-04 13:17:49.780: E/AndroidRuntime(1533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-04 13:17:49.780: E/AndroidRuntime(1533): at dalvik.system.NativeStart.main(Native Method)
12-04 13:17:49.780: E/AndroidRuntime(1533): Caused by: java.lang.NullPointerException
12-04 13:17:49.780: E/AndroidRuntime(1533): at com.example.shortcode.KeywordListView.onCreate(KeywordListView.java:111)
12-04 13:17:49.780: E/AndroidRuntime(1533): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-04 13:17:49.780: E/AndroidRuntime(1533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
最佳答案
我说避免调用 setContentView 两次。如果这样做,第一个将被覆盖。这可能就是您遇到异常(exception)的原因。尝试创建一个 xml 布局文件并在那里定义您需要的所有 View 。
关于android - 尝试在 android 中添加两个 setContentView 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13698206/