2014-02-19 53 views
4

我試圖添加頁腳到ListView,但是應用程序在此行代碼後崩潰:getListView().addView(footerView);。我是Android新手,這是我第一次使用ListView添加頁腳到ListView後崩潰

如果可能,我需要膨脹方法的簡短說明。

謝謝!

ToDoListAdapter mAdapter; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      // Create a new TodoListAdapter for this ListActivity's ListView 
      mAdapter = new ToDoListAdapter(getApplicationContext()); 

      // Put divider between ToDoItems and FooterView 
      getListView().setFooterDividersEnabled(true); 

      //TODO - Inflate footerView for footer_view.xml file 
      TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null); 

      //TODO - Add footerView to ListView 
      getListView().addView(footerView); 

      footerView.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        log("Entered footerView.OnClickListener.onClick()"); 

        //TODO - Attach Listener to FooterView. Implement onClick(). 
        Intent startNewActivity = new Intent(null, AddToDoActivity.class); 
        startActivityForResult(startNewActivity, ADD_TODO_ITEM_REQUEST); 

       } 
      }); 

      //TODO - Attach the adapter to this ListActivity's ListView 
      setListAdapter(mAdapter); 

     } 

登錄:

02-19 13:53:39.358: D/dalvikvm(1451): GC_FOR_ALLOC freed 68K, 6% free 3090K/3276K, paused 19ms, total 31ms 
02-19 13:53:39.366: I/dalvikvm-heap(1451): Grow heap (frag case) to 4.196MB for 1127532-byte allocation 
02-19 13:53:39.374: D/dalvikvm(1451): GC_FOR_ALLOC freed 2K, 5% free 4189K/4380K, paused 8ms, total 8ms 
02-19 13:53:39.402: D/AndroidRuntime(1451): Shutting down VM 
02-19 13:53:39.402: W/dalvikvm(1451): threadid=1: thread exiting with uncaught exception (group=0xa4d4db20) 
02-19 13:53:39.414: E/AndroidRuntime(1451): FATAL EXCEPTION: main 
02-19 13:53:39.414: E/AndroidRuntime(1451): Process: course.labs.todomanager, PID: 1451 
02-19 13:53:39.414: E/AndroidRuntime(1451): java.lang.RuntimeException: Unable to start activity ComponentInfo{course.labs.todomanager/course.labs.todomanager.ToDoManagerActivity}: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.os.Handler.dispatchMessage(Handler.java:102) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.os.Looper.loop(Looper.java:136) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at java.lang.reflect.Method.invoke(Method.java:515) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at dalvik.system.NativeStart.main(Native Method) 
02-19 13:53:39.414: E/AndroidRuntime(1451): Caused by: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.widget.AdapterView.addView(AdapterView.java:452) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at course.labs.todomanager.ToDoManagerActivity.onCreate(ToDoManagerActivity.java:56) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.app.Activity.performCreate(Activity.java:5231) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
02-19 13:53:39.414: E/AndroidRuntime(1451):  ... 11 more 

編輯:

謝謝,固定的這個問題,但現在我似乎有問題的地方在點擊收聽。

日誌:

02-19 14:19:02.346: I/Lab-UserInterface(1731): Entered footerView.OnClickListener.onClick() 
02-19 14:19:02.346: D/AndroidRuntime(1731): Shutting down VM 
02-19 14:19:02.346: W/dalvikvm(1731): threadid=1: thread exiting with uncaught exception (group=0xa4d4db20) 
02-19 14:19:02.350: E/AndroidRuntime(1731): FATAL EXCEPTION: main 
02-19 14:19:02.350: E/AndroidRuntime(1731): Process: course.labs.todomanager, PID: 1731 
02-19 14:19:02.350: E/AndroidRuntime(1731): java.lang.NullPointerException 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at android.content.ComponentName.<init>(ComponentName.java:77) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at android.content.Intent.<init>(Intent.java:3813) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at course.labs.todomanager.ToDoManagerActivity$1.onClick(ToDoManagerActivity.java:65) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at android.view.View.performClick(View.java:4438) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at android.view.View$PerformClick.run(View.java:18422) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at android.os.Handler.handleCallback(Handler.java:733) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at android.os.Handler.dispatchMessage(Handler.java:95) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at android.os.Looper.loop(Looper.java:136) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at java.lang.reflect.Method.invoke(Method.java:515) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
02-19 14:19:02.350: E/AndroidRuntime(1731):  at dalvik.system.NativeStart.main(Native Method) 
+2

'Intent startNewActivity = new Intent(null,AddToDoActivity.class);'爲什麼你有'null'?使用'MainActivity.this' – Raghunandan

+0

恩,謝謝。我錯過了。 –

+0

也發貼'footer_view.xml' – Raghunandan

回答

12
  1. 變化

    getListView().addView(footerView); 
    

    getListView().addFooterView(footerView); 
    
  2. 解釋爲充氣功能:

    LayoutInflater().inflate()只是將佈局xml轉換爲實際的View結構。

  3. 你的第二個問題:

    看一看點擊監聽:

    Intent startNewActivity = new Intent(null, AddToDoActivity.class); 
    

    你不能開始null作爲上下文的意圖(第一個參數)

    更改此:

    Intent startNewActivity = new Intent(ToDoManagerActivity.this, AddToDoActivity.class); 
    
+0

謝謝,作品!但仍然有錯誤,你能再看一次嗎? –

+1

請看看我編輯的答案。 –

+1

如果我的答案有幫助,請點擊左邊的複選標記以表示接受。 –

0

膨脹方法的簡短說明:將佈局XML文件實例化爲其相應的View對象。請參閱LayoutInflator