我有一个自定义类CustomDialog 有一个show()函数,它应该显示一个包含listview内容的对话框,对话框一直很好,直到我添加列表部分。
这里是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
答案 0 :(得分:1)
在你的Activity onCreate()中,我假设你用setContentView夸大了'elements_view.xml'以外的东西。 在show()中,通过搜索R.id.lvElements的Activity的视图层次结构来设置'list':
list = (ListView) act.findViewById(R.id.lvElements);
You Activity的布局不包含R.id.lvElements,因此list设置为null,因此当您尝试在其上设置适配器时,Null Pointer Exception。
您的列表视图在elements_view.xml中定义,您将其视为对话框的视图层次结构。将list的设置和用法移动到dialog.setContentView()之后的某个点并在对话框中调用findViewById(),而不是执行:
dialog.setContentView(R.layout.elements_view);
list = (ListView) dialog.findViewById(R.id.lvElements);
答案 1 :(得分: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);
然后设置适配器或类似的东西。 试试这个