2012-04-11 5 views
2

ViewStub에서 ListView를 만들려고하지만 NullPointerExeption을 계속 사용합니다. 내 버튼은 ViewStub 내부에서 의도 한대로 작동합니다. 실패한 코드는 다음과 같습니다.ViewStub에서 ListView를 사용할 때 NullPointerExeption

else if(v.getId() == R.id.comments_button_series_view){ 
    if(stubUsed == false){ 

     stub = (ViewStub) findViewById(R.id.stub); 
     View inflatedStub = stub.inflate(); 

     String[] comments = new String[]{"comment1", "comment2"}; 
     commentsAdapter = new ArrayAdapter<String>(this, 
            R.layout.patient_list_element, 
            R.id.comments_list_comments_view, comments); 
     commentsList = (ListView)findViewById(R.id.comments_list_comments_view); 
     commentsList.setAdapter(commentsAdapter); 

commentsList.setAdapter (commentsAdapter)에서 실패합니다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/comment_stub" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/settings_background" > 

    <Button 
     android:id="@+id/comments_button_comment_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="62dp" 
     android:text="Add comment" /> 


    <ListView 
     android:id="@+id/comments_list_comments_view" 
     android:layout_width="match_parent" 
     android:layout_height="470dp" 
     android:layout_alignLeft="@+id/pin_button_comments_view" 
     android:layout_below="@+id/comments_button_comment_view" > 

    </ListView> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignRight="@+id/comments_button_comment_view" 
     android:layout_marginRight="119dp" 
     android:text="COMMENTS" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/pin_button_comments_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/comments_button_comment_view" 
     android:layout_alignBottom="@+id/comments_button_comment_view" 
     android:layout_marginRight="30dp" 
     android:layout_toLeftOf="@+id/comments_button_comment_view" 
     android:text="Add Pin" /> 

    <Button 
     android:id="@+id/hide_button_comments_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/pin_button_comments_view" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="10dp" 
     android:text="Hide" /> 

</RelativeLayout> 

가 나는 viewstub를 사용하지 않고 ListView에 대한 코드를 테스트 한, 그것은 잘 작동 :

ListView에를 보유하고있는 ViewStub의 내용이 XML 파일은 다음과 같습니다.

나는 그것을 모두 가지고 있다고 생각하니? 어떤 도움을 주셔서 감사합니다! :)

편집 :

빠른 답장을 보내 주셔서 감사합니다!

contentView는 ViewStub가 확장 된보기에서 설정됩니다. 하지만 언급 한 바와 같이, 그것은 버튼으로 잘 작동하지만 ListView를 사용하여 nullpointer를 얻지는 않습니다.

04-11 10:37:55.400: W/dalvikvm(971):  threadid=1: thread exiting with uncaught exception (group=0x40014760) 
04-11 10:37:55.450: E/AndroidRuntime(971): FATAL EXCEPTION: main 
04-11 10:37:55.450: E/AndroidRuntime(971): java.lang.NullPointerException 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:396) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:366) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.AbsListView.obtainView(AbsListView.java:1964) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ListView.makeAndAddView(ListView.java:1756) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ListView.fillDown(ListView.java:656) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ListView.fillFromTop(ListView.java:716) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.ListView.layoutChildren(ListView.java:1609) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.AbsListView.onLayout(AbsListView.java:1794) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.FrameLayout.onLayout(FrameLayout.java:400) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.LinearLayout.onLayout(LinearLayout.java:1314) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.widget.FrameLayout.onLayout(FrameLayout.java:400) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.View.layout(View.java:9330) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewGroup.layout(ViewGroup.java:3795) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewRoot.performTraversals(ViewRoot.java:1201) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.view.ViewRoot.handleMessage(ViewRoot.java:1944) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.os.Looper.loop(Looper.java:126) 
04-11 10:37:55.450: E/AndroidRuntime(971): at android.app.ActivityThread.main(ActivityThread.java:3997) 
04-11 10:37:55.450: E/AndroidRuntime(971): at java.lang.reflect.Method.invokeNative(Native Method) 
04-11 10:37:55.450: E/AndroidRuntime(971): at java.lang.reflect.Method.invoke(Method.java:491) 
04-11 10:37:55.450: E/AndroidRuntime(971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
04-11 10:37:55.450: E/AndroidRuntime(971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
04-11 10:37:55.450: E/AndroidRuntime(971): at dalvik.system.NativeStart.main(Native Method) 

다시 한 번 감사드립니다 :)

답변

1

당신이 ArrayAdapter 개체를 만들 때 오류가있는 것 같습니다 : :(

다음은 로그 캣에서 결과입니다. 생성자 (textViewResourceId)의 세 번째 매개 변수는 ListView 대신 TextView이어야합니다.

변경하십시오. 작동해야합니다.

+0

아, 마지막. 감사합니다! 한 달 만에 했어! – user1326621