- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在pager.setAdapter(tabpager);
这是代码,请检查一下,让我知道错误
主要 Activity 的 Java 代码
package com.example.prototype;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
TabsPagerAdapter tabpager;
ViewPager pager;
DrawerLayout drawerLayout;
ActionBar action_bar;
ListView navigation_list;
@SuppressWarnings("deprecation")
ActionBarDrawerToggle drawerToggle;
String items[];
String tabs_list[]={"Best Offers","Categories","Near By Places"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
initComponents();
action_bar=getActionBar();
setNavigationList();
setTabs();
}
@SuppressWarnings("deprecation")
private void setTabs() {
action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs_list) {
action_bar.addTab(action_bar.newTab().setText(tab_name)
.setTabListener(this));
}
try{pager.setAdapter(tabpager);}
catch(Exception e){
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
private void setNavigationList() {
int open=R.string.navigation_open;
int close=R.string.navigation_close;
navigation_list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
drawerToggle=new ActionBarDrawerToggle(this,drawerLayout, R.drawable.ic_menu_white_18dp,open,close){
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
action_bar.show();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
action_bar.hide();
}
};
drawerLayout.setDrawerListener(drawerToggle);
action_bar.setDisplayHomeAsUpEnabled(true);
action_bar.setHomeButtonEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
private void initComponents() {
// TODO Auto-generated method stub
drawerLayout=(DrawerLayout) findViewById(R.id.drawer_layout);
action_bar=getActionBar();
navigation_list=(ListView) findViewById(R.id.left_drawer);
items=getResources().getStringArray(R.array.navigation_drawer);
pager=(ViewPager) findViewById(R.id.pager);
android.support.v4.app.FragmentManager m=getSupportFragmentManager();
tabpager=new TabsPagerAdapter(m);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.search_menu) {
Toast.makeText(getApplicationContext(), "Searched Clicked ", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.share_menu) {
Toast.makeText(getApplicationContext(), "Share Clicked ", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.navigation_menu) {
Toast.makeText(getApplicationContext(), "Navigation Clicked ", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
对于适配器类
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
switch(arg0){
case 0: new BestOfferFragment();
break;
case 1: new CategoriesrFragment();
break;
case 2: new NearByPlacesFragment();
break;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true" />
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="5dp"
android:background="#E1E1E1"/>
</android.support.v4.widget.DrawerLayout>
请帮帮我
我检查了 Viewer 和 Tabadapter 的对象,但它们不是空的,所以我不明白为什么它在设置适配器时出错..
这是日志
07-06 01:24:16.471: D/dalvikvm(20183): threadid=2: interp stack at 0x5a6b7000
07-06 01:24:16.476: D/dalvikvm(20183): threadid=3: interp stack at 0x5a7bf000
07-06 01:24:16.477: D/jdwp(20183): prepping for JDWP over ADB
07-06 01:24:16.477: D/jdwp(20183): ADB transport startup
07-06 01:24:16.479: D/dalvikvm(20183): Elevating priority from 0 to -8
07-06 01:24:16.479: D/dalvikvm(20183): threadid=4: interp stack at 0x5a8c7000
07-06 01:24:16.480: D/jdwp(20183): JDWP: thread running
07-06 01:24:16.482: D/jdwp(20183): acceptConnection
07-06 01:24:16.483: D/jdwp(20183): trying to receive file descriptor from ADB
07-06 01:24:16.484: D/dalvikvm(20183): threadid=5: interp stack at 0x5cd1d000
07-06 01:24:16.484: D/dalvikvm(20183): zygote get thread init done
07-06 01:24:16.484: D/dalvikvm(20183): create interp thread : stack size=32KB
07-06 01:24:16.484: D/dalvikvm(20183): create new thread
07-06 01:24:16.484: D/dalvikvm(20183): new thread created
07-06 01:24:16.485: D/dalvikvm(20183): update thread list
07-06 01:24:16.485: D/dalvikvm(20183): threadid=6: interp stack at 0x5cd25000
07-06 01:24:16.485: D/dalvikvm(20183): threadid=6: created from interp
07-06 01:24:16.485: D/dalvikvm(20183): start new thread
07-06 01:24:16.486: D/dalvikvm(20183): create interp thread : stack size=32KB
07-06 01:24:16.486: D/dalvikvm(20183): create new thread
07-06 01:24:16.487: D/dalvikvm(20183): new thread created
07-06 01:24:16.487: D/dalvikvm(20183): update thread list
07-06 01:24:16.487: D/dalvikvm(20183): threadid=6: notify debugger
07-06 01:24:16.487: D/dalvikvm(20183): threadid=6 (ReferenceQueueDaemon): calling run()
07-06 01:24:16.487: D/dalvikvm(20183): threadid=7: interp stack at 0x5ce2d000
07-06 01:24:16.488: D/dalvikvm(20183): threadid=7: created from interp
07-06 01:24:16.488: D/dalvikvm(20183): start new thread
07-06 01:24:16.490: D/dalvikvm(20183): create interp thread : stack size=32KB
07-06 01:24:16.490: D/dalvikvm(20183): create new thread
07-06 01:24:16.490: D/jdwp(20183): received file descriptor 39 from ADB
07-06 01:24:16.490: D/dalvikvm(20183): new thread created
07-06 01:24:16.490: D/dalvikvm(20183): update thread list
07-06 01:24:16.490: D/dalvikvm(20183): threadid=7: notify debugger
07-06 01:24:16.490: D/dalvikvm(20183): threadid=7 (FinalizerDaemon): calling run()
07-06 01:24:16.491: D/dalvikvm(20183): threadid=8: interp stack at 0x5cf35000
07-06 01:24:16.491: D/dalvikvm(20183): threadid=8: created from interp
07-06 01:24:16.491: D/dalvikvm(20183): start new thread
07-06 01:24:16.495: D/jdwp(20183): processIncoming
07-06 01:24:16.495: D/jdwp(20183): processIncoming
07-06 01:24:16.495: D/jdwp(20183): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x4000019F, flags=0x0, dataLen=0x8
07-06 01:24:16.504: D/jdwp(20183): processIncoming
07-06 01:24:16.504: D/jdwp(20183): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x400001A0, flags=0x0, dataLen=0xC
07-06 01:24:16.516: D/jdwp(20183): processIncoming
07-06 01:24:16.517: D/jdwp(20183): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400001A1, flags=0x0, dataLen=0x8
07-06 01:24:16.525: D/dalvikvm(20183): threadid=8: notify debugger
07-06 01:24:16.525: D/dalvikvm(20183): threadid=8 (FinalizerWatchdogDaemon): calling run()
07-06 01:24:16.537: D/jdwp(20183): processIncoming
07-06 01:24:16.537: D/jdwp(20183): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400001A2, flags=0x0, dataLen=0x8
07-06 01:24:16.538: D/jdwp(20183): processIncoming
07-06 01:24:16.538: D/jdwp(20183): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x400001A3, flags=0x0, dataLen=0x9
07-06 01:24:16.576: D/dalvikvm(20183): threadid=9: interp stack at 0x5d33b000
07-06 01:24:16.591: D/dalvikvm(20183): threadid=10: interp stack at 0x5d443000
07-06 01:24:16.614: D/jdwp(20183): sendBufferedRequest : len=0x3D
07-06 01:24:16.720: D/jdwp(20183): sendBufferedRequest : len=0x45
07-06 01:24:16.770: D/dalvikvm(20183): open_cached_dex_file : /data/app/com.example.prototype-2.apk /data/dalvik-cache/data@app@com.example.prototype-2.apk@classes.dex
07-06 01:24:16.807: D/skia(20183): Flag is not 10
07-06 01:24:16.813: D/skia(20183): Flag is not 10
07-06 01:24:16.817: D/skia(20183): Flag is not 10
07-06 01:24:16.824: D/skia(20183): Flag is not 10
07-06 01:24:16.825: D/skia(20183): Flag is not 10
07-06 01:24:16.827: D/skia(20183): Flag is not 10
07-06 01:24:16.828: D/skia(20183): Flag is not 10
07-06 01:24:16.845: D/skia(20183): Flag is not 10
07-06 01:24:16.857: D/dalvikvm(20183): GC_FOR_ALLOC freed 109K, 8% free 2861K/3092K, paused 11ms, total 11ms
07-06 01:24:16.859: I/dalvikvm-heap(20183): Grow heap (frag case) to 3.982MB for 1127536-byte allocation
07-06 01:24:16.876: D/dalvikvm(20183): GC_FOR_ALLOC freed 1K, 6% free 3961K/4196K, paused 17ms, total 17ms
07-06 01:24:16.884: D/skia(20183): Flag is not 10
07-06 01:24:16.893: D/skia(20183): Flag is not 10
07-06 01:24:16.896: D/dalvikvm(20183): GC_CONCURRENT freed 3K, 6% free 3969K/4196K, paused 2ms+1ms, total 20ms
07-06 01:24:16.899: D/skia(20183): Flag is not 10
07-06 01:24:16.900: D/skia(20183): Flag is not 10
07-06 01:24:16.901: D/skia(20183): Flag is not 10
07-06 01:24:16.902: D/skia(20183): Flag is not 10
07-06 01:24:16.904: D/AbsListView(20183): checkAbsListViewlLogProperty get invalid command
07-06 01:24:16.907: D/skia(20183): Flag is not 10
07-06 01:24:16.910: D/skia(20183): Flag is not 10
07-06 01:24:16.914: D/skia(20183): Flag is not 10
07-06 01:24:16.916: D/skia(20183): Flag is not 10
07-06 01:24:16.917: D/skia(20183): Flag is not 10
07-06 01:24:16.918: D/skia(20183): Flag is not 10
07-06 01:24:16.919: D/skia(20183): Flag is not 10
07-06 01:24:16.976: D/AndroidRuntime(20183): Shutting down VM
07-06 01:24:16.976: W/dalvikvm(20183): threadid=1: thread exiting with uncaught exception (group=0x4126e9a8)
07-06 01:24:16.980: E/AndroidRuntime(20183): FATAL EXCEPTION: main
07-06 01:24:16.980: E/AndroidRuntime(20183): java.lang.NullPointerException
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:417)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:412)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:869)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.support.v4.view.ViewPager.populate(ViewPager.java:1019)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.View.measure(View.java:15609)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.View.measure(View.java:15609)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.View.measure(View.java:15609)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.View.measure(View.java:15609)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-06 01:24:16.980: E/AndroidRuntime(20183): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2191)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.View.measure(View.java:15609)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.Choreographer.doFrame(Choreographer.java:548)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.os.Handler.handleCallback(Handler.java:725)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.os.Looper.loop(Looper.java:153)
07-06 01:24:16.980: E/AndroidRuntime(20183): at android.app.ActivityThread.main(ActivityThread.java:5297)
07-06 01:24:16.980: E/AndroidRuntime(20183): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 01:24:16.980: E/AndroidRuntime(20183): at java.lang.reflect.Method.invoke(Method.java:511)
07-06 01:24:16.980: E/AndroidRuntime(20183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-06 01:24:16.980: E/AndroidRuntime(20183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-06 01:24:16.980: E/AndroidRuntime(20183): at dalvik.system.NativeStart.main(Native Method)
07-06 01:24:17.008: I/Process(20183): Sending signal. PID: 20183 SIG: 9
最佳答案
您的适配器 getItem()
没有返回任何非空值。返回空值会导致问题。
请注意,您正在使用 new
实例化新 fragment ,但您并未对刚刚实例化的 fragment 的引用执行任何操作。
要修复它,改变
new WhateverFragment();
到
return new WhateverFragment();
对于适配器中的所有三个 fragment 。
关于android - pager.setAdapter() 中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31234351/
我刚接触 C 语言几周,所以对它还很陌生。 我见过这样的事情 * (variable-name) = -* (variable-name) 在讲义中,但它到底会做什么?它会否定所指向的值吗? 最佳答案
我有一个指向内存地址的void 指针。然后,我做 int 指针 = void 指针 float 指针 = void 指针 然后,取消引用它们以获取值。 { int x = 25; vo
我正在与计算机控制的泵进行一些串行端口通信,我用来通信的 createfile 函数需要将 com 端口名称解析为 wchar_t 指针。 我也在使用 QT 创建一个表单并获取 com 端口名称作为
#include "stdio.h" #include "malloc.h" int main() { char*x=(char*)malloc(1024); *(x+2)=3; --
#include #include main() { int an_int; void *void_pointer = &an_int; double *double_ptr = void
对于每个时间步长,我都有一个二维矩阵 a[ix][iz],ix 从 0 到 nx-1 和 iz 从 0 到 nz-1。 为了组装所有时间步长的矩阵,我定义了一个长度为 nx*nz*nt 的 3D 指针
我有一个函数,它接受一个指向 char ** 的指针并用字符串填充它(我猜是一个字符串数组)。 *list_of_strings* 在函数内部分配内存。 char * *list_of_strings
我试图了解当涉及到字符和字符串时,内存分配是如何工作的。 我知道声明的数组的名称就像指向数组第一个元素的指针,但该数组将驻留在内存的堆栈中。 另一方面,当我们想要使用内存堆时,我们使用 malloc,
我有一个 C 语言的 .DLL 文件。该 DLL 中所有函数所需的主要结构具有以下形式。 typedef struct { char *snsAccessID; char *
指针, C语言的精髓 莫队先咕几天, 容我先讲完树剖 (因为后面树上的东西好多都要用树剖求 LCA). 什么是指针 保存变量地址的变量叫做指针. 这是大概的定义, 但是Defad认为
我得到了以下数组: let arr = [ { children: [ { children: [], current: tru
#include int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); for (i=0;
我正在编写一个程序,它接受一个三位数整数并将其分成两个整数。 224 将变为 220 和 4。 114 将变为 110 和 4。 基本上,您可以使用模数来完成。我写了我认为应该工作的东西,编译器一直说
好吧,我对 C++ 很陌生,我确定这个问题已经在某个地方得到了回答,而且也很简单,但我似乎找不到答案.... 我有一个自定义数组类,我将其用作练习来尝试了解其工作原理,其定义如下: 标题: class
1) this 指针与其他指针有何不同?据我了解,指针指向堆中的内存。如果有指向它们的指针,这是否意味着对象总是在堆中构造? 2)我们可以在 move 构造函数或 move 赋值中窃取this指针吗?
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C : pointer to struct in the struct definition 在我的初学者类
我有两个指向指针的结构指针 typedef struct Square { ... ... }Square; Square **s1; //Representing 2D array of say,
变量在内存中是如何定位的?我有这个代码 int w=1; int x=1; int y=1; int z=1; int main(int argc, char** argv) { printf
#include #include main() { char *q[]={"black","white","red"}; printf("%s",*q+3); getch()
我在“C”类中有以下函数 class C { template void Func1(int x); template void Func2(int x); }; template void
我是一名优秀的程序员,十分优秀!