我刚添加了一条确认消息,用于删除android中的联系人 我的代码没有任何错误,但当我点击删除按钮时,程序关闭并给我以下错误。
> 03-27 04:30:32.309: E/AndroidRuntime(1855): FATAL EXCEPTION: main
> 03-27 04:30:32.309: E/AndroidRuntime(1855):
> android.view.WindowManager$BadTokenException: Unable to add window --
> token null is not for an application 03-27 04:30:32.309:
> E/AndroidRuntime(1855): at
> android.view.ViewRootImpl.setView(ViewRootImpl.java:571) 03-27
> 04:30:32.309: E/AndroidRuntime(1855): at
> android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
> 03-27 04:30:32.309: E/AndroidRuntime(1855): at
> android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
> 03-27 04:30:32.309: E/AndroidRuntime(1855): at
> android.app.Dialog.show(Dialog.java:281) 03-27 04:30:32.309:
> E/AndroidRuntime(1855): at
> com.example.android.ViewDetail$1.onClick(ViewDetail.java:95) 03-27
> 04:30:32.309: E/AndroidRuntime(1855): at
> android.view.View.performClick(View.java:4202) 03-27 04:30:32.309:
> E/AndroidRuntime(1855): at
> android.view.View$PerformClick.run(View.java:17340) 03-27
> 04:30:32.309: E/AndroidRuntime(1855): at
> android.os.Handler.handleCallback(Handler.java:725) 03-27
> 04:30:32.309: E/AndroidRuntime(1855): at
> android.os.Handler.dispatchMessage(Handler.java:92) 03-27
> 04:30:32.309: E/AndroidRuntime(1855): at
> android.os.Looper.loop(Looper.java:137) 03-27 04:30:32.309:
> E/AndroidRuntime(1855): at
> android.app.ActivityThread.main(ActivityThread.java:5039) 03-27
> 04:30:32.309: E/AndroidRuntime(1855): at
> java.lang.reflect.Method.invokeNative(Native Method) 03-27
> 04:30:32.309: E/AndroidRuntime(1855): at
> java.lang.reflect.Method.invoke(Method.java:511) 03-27 04:30:32.309:
> E/AndroidRuntime(1855): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
> 03-27 04:30:32.309: E/AndroidRuntime(1855): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-27
> 04:30:32.309: E/AndroidRuntime(1855): at
> dalvik.system.NativeStart.main(Native Method)
这是我刚添加并导致错误的部分。
//delete button
deleteb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getBaseContext());
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Intent intent = new Intent (ViewDetail.this, MainActivity.class);
intent.putExtra("deleteButton","delete");
intent.putExtra("ContactID",getContactID);
startActivity(intent);
ViewDetail.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
有人可以帮我这个吗?
Eddited。这是我的MainActivity页面
package com.example.android;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
abcDBHelper xHelper;
ListView lv;
Button btnAdd;
List<Contact> getAllContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//portarait mode
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(getApplicationContext(), "PORTRAIT MODE",Toast.LENGTH_LONG).show();
}
//landscape mode
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(getApplicationContext(), "LANDSCAPE MODE",Toast.LENGTH_LONG).show();
}
Button addNew = (Button)findViewById(R.id.add);
addNew.setOnClickListener(this);
xHelper = new abcDBHelper(this);
lv = (ListView)findViewById(R.id.listView);
btnAdd = (Button)findViewById(R.id.add);
Bundle passData;
//button click to execute following function
if(getIntent().hasExtra("deleteButton"))
{
onClickDelete();
}
if(getIntent().hasExtra("createButton"))
{
onClickInsert();
}
getAllContact = getAllContact();
List<String> showList = new ArrayList<String>();
for (int i= 0; i<getAllContact.size(); i++){
showList.add(getAllContact.get(i).getFullName());
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,showList);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id){
long ContactID = getAllContact.get((int)id).getContactID();
onClickSelect(ContactID);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//show contact
public List<Contact> getAllContact(){
List<Contact> contacts = new ArrayList<Contact>();
SQLiteDatabase db;
String cmd;
db = xHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM detail;", null);
while(cursor.moveToNext()){
int ContactID = Integer.parseInt(cursor.getString(0));
String FamilyName = cursor.getString(1);
String FirstName = cursor.getString(2);
int HouseNumber = Integer.parseInt(cursor.getString(3));
String Street = cursor.getString(4);
String Town = cursor.getString(5);
String Country = cursor.getString(6);
String Postcode = cursor.getString(7);
int Telephone = Integer.parseInt(cursor.getString(8));
Contact newContact = new Contact(ContactID,FamilyName,FirstName,HouseNumber,Street,Town ,Country ,Postcode,Telephone);
contacts.add(newContact);
}
return contacts;
}
//insert new contact to database
public void onClickInsert() {
SQLiteDatabase db;
String cmd;
db = xHelper.getWritableDatabase();
Bundle bundle = getIntent().getExtras();
String getFamName = bundle.getString("dbFamilyName" );
String getFirName = bundle.getString("dbFirstName" );
int getHouseNo = bundle.getInt ("dbHouseNumber" );
String getStreet = bundle.getString("dbStreet" );
String getTown = bundle.getString("dbTown" );
String getCountry = bundle.getString("dbCountry" );
String getPostcode = bundle.getString("dbPostcode");
int getTelNo = bundle.getInt ("dbTelephone" );
//insert
ContentValues pStatement = new ContentValues();
pStatement.put ("dbFamilyName",getFamName);
pStatement.put ("dbFirstName",getFirName);
pStatement.put ("dbHouseNumber",getHouseNo);
pStatement.put ("dbStreet",getStreet);
pStatement.put ("dbTown",getTown);
pStatement.put ("dbCountry",getCountry);
pStatement.put ("dbPostcode",getPostcode);
pStatement.put ("dbTelephone",getTelNo);
db.insert("detail",null,pStatement);
Toast.makeText(getApplicationContext(), "Add Successfully", Toast.LENGTH_LONG).show();
xHelper.close();
}
//delete contact
public void onClickDelete() {
SQLiteDatabase db;
db = xHelper.getWritableDatabase();
Bundle bundle =getIntent().getExtras();
int ContactID = bundle.getInt("ContactID");
db.delete ("detail","_id"+" = ?", new String[]{String.valueOf(ContactID)} );
Toast.makeText(getApplicationContext(), "Delete Successfully", Toast.LENGTH_LONG).show();
xHelper.close();
}
//select clicked contact
public void onClickSelect(long ContactID) {
SQLiteDatabase db;
String cmd;
db = xHelper.getReadableDatabase();
Cursor cursor;
cursor = db.rawQuery("SELECT * FROM detail WHERE _id = "+ContactID, null);
String Result = "";
while(cursor.moveToNext()){
int ContactID2 = Integer.parseInt(cursor.getString(0));
String FamilyName = cursor.getString(1);
String FirstName = cursor.getString(2);
int HouseNumber = Integer.parseInt(cursor.getString(3));
String Street = cursor.getString(4);
String Town = cursor.getString(5);
String Country = cursor.getString(6);
String Postcode = cursor.getString(7);
int Telephone = Integer.parseInt(cursor.getString(8));
Intent intent = new Intent (MainActivity.this,ViewDetail.class);
intent.putExtra("dbContactID", ContactID2);
intent.putExtra ("dbFamilyName", FamilyName);
intent.putExtra ("dbFirstName", FirstName);
intent.putExtra ("dbHouseNumber", HouseNumber);
intent.putExtra ("dbStreet", Street);
intent.putExtra ("dbTown", Town);
intent.putExtra ("dbCountry", Country);
intent.putExtra ("dbPostcode", Postcode);
intent.putExtra ("dbTelephone", Telephone);
startActivity(intent);
Result += (ContactID2);
}
cursor.close();
xHelper.close();
}
//SQLiteOpenHelper
class abcDBHelper extends SQLiteOpenHelper {
public abcDBHelper(Context context) {
super(context, "user_info.db", null, 2);
Log.d("Database Operation","Databse created");
}
@Override
public void onCreate(SQLiteDatabase db) {
// create table
db.execSQL("CREATE TABLE detail (_id INTEGER PRIMARY KEY, dbFamilyName TEXT, dbFirstName TEXT , dbHouseNumber INTEGER, dbStreet TEXT, dbTown TEXT, dbCountry TEXT, dbPostcode TEXT, dbTelephone INTEGER);");
Log.d("Database Operation","Table created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS detail");
onCreate(db);
}
}
// navigate
@Override
public void onClick(View v) {
if(v.getId()== R.id.add){
Intent a = new Intent (MainActivity.this, CreateContact.class);
startActivity(a);
}
}
}
答案 0 :(得分:0)
活动上下文缺少AlertDialog,这就是令牌为空的原因
如果您在片段中使用意味着调用
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
活动
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
答案 1 :(得分:0)
只需更改上下文
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getBaseContext());
到
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
ViewDetail.this);