插入自定义列表视图时出现错误Android SlidingDrawer按下列表视图上的按钮。但是,如果使用相同的源,则可以使用片段。这是为什么?
这是SlidingDrawer XML。
<SlidingDrawer
android:id="@+id/slidingDraw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/content"
android:handle="@+id/handle"
android:visibility="visible">
<Button
android:id="@+id/handle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Memo Check"/>
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="#ffffff">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView2">
</ListView>
</LinearLayout>
</SlidingDrawer>
这是listview自定义适配器
public View getView(final int position, View convertView, ViewGroup parent) {
cursor.moveToPosition(position);
if(convertView==null){
convertView = inflater.inflate(item_layout,null);
}
Button btn = (Button)convertView.findViewById(R.id.Okbtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cursor.moveToPosition(position);
dialogView = (View) inflater.inflate(R.layout.memo_layout, null);
TextView tv = (TextView) dialogView.findViewById(R.id.textView9);
tv.setText(cursor.getString(cursor.getColumnIndex("_memo")));
AlertDialog.Builder dlg = new AlertDialog.Builder(context);
dlg.setTitle("Memo Check");
dlg.setView(dialogView);
dlg.setPositiveButton("OK", null);
dlg.show();
}
});
TextView tv1 = (TextView)convertView.findViewById(R.id.textView7);
tv1.setText(cursor.getString(cursor.getColumnIndex("_date")));
TextView tv2 = (TextView)convertView.findViewById(R.id.textView8);
tv2.setText(cursor.getString(cursor.getColumnIndex("_time")));
return convertView;
}
这是错误代码。
FATAL EXCEPTION: main
Process: com.example.user.lockscreen, PID: 1982
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:319)
at com.example.user.lockscreen.Adapter.myAdapter2$1.onClick(myAdapter2.java:72)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:616)
myAdapter2.java:72是listview自定义适配器的dlg.show()。