2012-12-04 72 views
2

我创建一个自定义的ListView其中我的ListView xml是keywordList .also我创建自定义导航栏编程使用教程http://mtmurdockblog.com/2011/10/17/navigation-bar-for-android/。因为有两个setContentView()一个用于ListView xml,另一个用于导航栏。如果有异常,您可以帮助我下面的代码。空指针异常时尝试添加两个setContentView在android

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); 

错误的logcat: -

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) 
+0

你认为从logcat的堆栈跟踪可能是有用的? – Simon

+0

当然会发布你的... – user578386

+0

发布日志猫 –

回答

1

我说避免调用的setContentView两次。如果你这样做,首先会被覆盖。这可能是你获得例外的原因。尝试创建一个xml布局文件并在那里定义所需的所有视图。

+1

这是我的博客,我批准此消息。 ;) – mtmurdock

1

再次检查教程,他只用了一次setContentView。 ll.addView(nb); setContentView(ll); 你应该在listView中加载关键字,然后在listView中使用 li.addView(navigationBar)添加导航栏;

OR

您可以导航使用XML,然后可以添加naviation栏下方的列表视图。之后,以编程方式在listview中加载关键字。我做了这样的观点,看看这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:weightSum="100" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="9" 
        android:background="@drawable/header" > 

     <ImageView 
      android:id="@+id/imageViewHome" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_gravity="center" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:scaleType="fitCenter" 
      android:src="@drawable/home" /> 

     <TextView 
      android:id="@+id/textViewTitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:text="Groups" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="@android:color/white" 
      android:textStyle="bold" /> 

     <ImageView 
      android:id="@+id/imageViewCreate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="5dp" 
      android:layout_toLeftOf="@+id/imageViewDelete" 
      android:src="@drawable/add" /> 

     <ImageView 
      android:id="@+id/imageViewDelete" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="5dp" 
      android:scaleType="fitXY" 
      android:src="@drawable/delete" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="9" 
     android:background="@drawable/bg_gradient_grey" > 

     <EditText 
      android:id="@+id/editTextSearch" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="15dp" 
      android:layout_marginRight="15dp" 
      android:layout_marginTop="5dp" 
      android:background="@drawable/rounded_edges" 
      android:ellipsize="end" 
      android:ems="15" 
      android:hint="Search" 
      android:singleLine="true" 
      android:textSize="15dp" > 

      <requestFocus /> 
     </EditText> 

     <ImageButton 
      android:id="@+id/imageButtonSearch" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:layout_alignWithParentIfMissing="true" 
      android:layout_centerInParent="false" 
      android:layout_centerVertical="true" 
      android:layout_gravity="center" 
      android:layout_marginRight="25dp" 
      android:background="@null" 
      android:src="@drawable/ic_action_search" /> 
    </RelativeLayout> 

    <ListView 
     android:id="@+id/groupsList" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="82" 
     android:background="@android:color/white" 
     android:cacheColorHint="#00000000" > 
    </ListView> 
</LinearLayout> 

+0

肯定会检查了这一点... ... – user578386