2014-01-17 69 views
0

我有一個自定義的類CustomDialog 有一個show()函數,應該顯示一個對話框與列表視圖的內容,對話框apears罰款,直到我添加列表部分。在對話框中添加一個列表視圖

這裏是elements_view XML代碼:

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

這裏是element_layout:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:background="@drawable/circuit_board"> 
    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="21dp" 
    android:layout_marginTop="14dp" 
    android:text="Medium Text" 
    /> 

    <ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/ic_launcher" /> 

    </RelativeLayout> 

這裏是我的CustomDialog類:

package com.example.control; 
import java.util.ArrayList; 
import com.example.control.R; 
import android.app.ActionBar.LayoutParams; 
import android.app.Activity; 
import android.app.Dialog; 
import android.content.Context; 
import android.graphics.Color; 
import android.graphics.drawable.ColorDrawable; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 


public class CustomDialog { 

private Context con; 
    private Activity act; 
ListView list; 
    private ArrayList<String> listData; 
static final String[] str = new String[] { "Apple", "Avocado", "Banana", 
    "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit", 
    "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" }; 

    CustomDialog(Activity activity) throws Exception{ 
this.act = activity; 
this.con = activity; 
} 

    void show() throws Exception{ 
try{ 

     try{ 
list = (ListView) act.findViewById(R.id.lvElements); 


ArrayAdapter<String> ad = new ArrayAdapter<String>(con,R.layout.element_layout,str); 
    list.setAdapter(ad); 
    } 
    catch(Exception ex){ 
android.util.Log.e("Control", "problem in list "); 
android.util.Log.e("Control", ex.toString()); 

    } 

    final Dialog dialog = new Dialog(con); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.elements_view); 
     final Window window = dialog.getWindow(); 
     window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,   WindowManager.LayoutParams.MATCH_PARENT); 
     window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 
     window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
     dialog.show(); 
} 
catch(Exception ex){ 
    throw ex; 

} 

    } 


    }//ends the class 

內嘗試引起在logcat中獲取此對話框時不顯示:

01-17 01:54:50.122: E/Control(29502): problem in list 
01-17 01:54:50.122: E/Control(29502): java.lang.NullPointerException 
01-17 01:54:50.152: D/dalvikvm(29502): GC_FOR_ALLOC freed 3096K, 43% free 10009K/17364K, paused 24ms, total 27ms 
01-17 01:54:50.162: I/dalvikvm-heap(29502): Grow heap (frag case) to 16.203MB for  3464956-byte allocation 
01-17 01:54:50.202: D/dalvikvm(29502): GC_CONCURRENT freed 18K, 36% free 13374K/20748K,  paused 5ms+2ms, total 42ms 
01-17 01:54:50.272: D/AbsListView(29502): Get MotionRecognitionManager 
01-17 01:54:50.292: D/dalvikvm(29502): GC_CONCURRENT freed 3386K, 32% free 11922K/17368K, paused 2ms+15ms, total 36ms 
01-17 01:54:50.302: D/AbsListView(29502): unregisterIRListener() is called 
01-17 01:54:50.312: D/AbsListView(29502): unregisterIRListener() is called 
01-17 01:54:50.352: D/AbsListView(29502): unregisterIRListener() is called 

回答

1

在您的Activity onCreate()中,我假設您使用setContentView擴充了除'elements_view.xml'之外的其他東西。 在秀(),您可以通過搜索活動的視圖層次結構R.id.lvElements設置「列表」:

list = (ListView) act.findViewById(R.id.lvElements); 

所以列表設置爲空你活動的佈局不包含R.id.lvElements,因此當您嘗試在其上設置適配器時,空指針異常。

您的列表視圖在您作爲對話框的視圖層次結構膨脹的elements_view.xml中定義。移動列表的設置和使用,以dialog.setContentView(後點),並在對話框中調用findViewById(),不採取行動:

dialog.setContentView(R.layout.elements_view); 
list = (ListView) dialog.findViewById(R.id.lvElements); 
+0

謝謝,把數組適配器和listview放在dialog.setContentView(R.layout.elements_view)之後;解決了第一個問題。但然後我不得不使用這個構造函數的適配器ArrayAdapter(上下文上下文,int資源,int textViewResourceId,列表對象),解決了一切! – NoVa

1

首先你只需要Inflate Dialog Layout象下面這樣:

private static LayoutInflater inflater = null; 
inflater = (LayoutInflater) activity 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

現在,創建全球Dialog對象:

Dialog main_dialog; 

現在,虛增您Dialog Layout View並設置爲Dialog

final View DialogView = inflater.inflate(
      R.layout.elements_view, null); 

main_dialog = new Dialog(Splash.this,R.style.Theme_Dialog); 

main_dialog.setContentView(DialogView); 

現在,您的列表視圖在elements_view.xml中定義,您將充氣爲對話框的視圖層次結構。從你的DialogView中找到列表視圖

list = (ListView)DialogView.findViewById(R.id.lvElements); 

並設置適配器之後還是這樣的東西。 試試這個

相關問題