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