我的应用程序崩溃和logcat显示此问题(任何人都可以告诉我我的问题在哪里?我可以在有人需要时显示我的应用程序代码):
07-15 15:02:57.128: W/dalvikvm(29804): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
07-15 15:02:57.128: E/AndroidRuntime(29804): FATAL EXCEPTION: main
07-15 15:02:57.128: E/AndroidRuntime(29804): java.lang.IllegalArgumentException: View not attached to window manager
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:587)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:324)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:151)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.app.Dialog.dismissDialog(Dialog.java:321)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.app.Dialog$1.run(Dialog.java:119)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.app.Dialog.dismiss(Dialog.java:306)
07-15 15:02:57.128: E/AndroidRuntime(29804): at com.flex.sklepik.Z_Glowna$DodajZakupy.onPostExecute(Z_Glowna.java:333)
07-15 15:02:57.128: E/AndroidRuntime(29804): at com.flex.sklepik.Z_Glowna$DodajZakupy.onPostExecute(Z_Glowna.java:1)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.os.AsyncTask.finish(AsyncTask.java:602)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.os.AsyncTask.access$600(AsyncTask.java:156)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.os.Looper.loop(Looper.java:137)
07-15 15:02:57.128: E/AndroidRuntime(29804): at android.app.ActivityThread.main(ActivityThread.java:4441)
07-15 15:02:57.128: E/AndroidRuntime(29804): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 15:02:57.128: E/AndroidRuntime(29804): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 15:02:57.128: E/AndroidRuntime(29804): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-15 15:02:57.128: E/AndroidRuntime(29804): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-15 15:02:57.128: E/AndroidRuntime(29804): at dalvik.system.NativeStart.main(Native Method)
我的代码看起来像这样(我真的不知道bug在哪里)任何人都可以用这个废话来帮助我吗?:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class Z_Glowna extends Activity {
private AtomPayListAdapter adapter;
private ProgressDialog pDialog;
private String nazwa, ilosc, id, lat, longi, name;
private double il;
public JSONObject json;
private int sprawdz;
private static final String TAG_MARKET = "nazwa";
private static final String TAG_POSTS = "posts";
private static final String TAG_SZER = "ilosc";
private static final String TAG_USERS = "users";
private static final String TAG_ID = "id";
double Lzakupow[];
double test[];
double ile[];
String zakup[];
int b = 0;
int y = 0;
private JSONArray zakupy = null;
private ArrayList<HashMap<String, String>> mListaMarketow;
// ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
setupListViewAdapter();
setupAddPaymentButton();
Button but = (Button) findViewById(R.id.but);
new WczytajZakupy().execute();
/*
* but.setOnClickListener(new Button.OnClickListener() {
*
* public void onClick(View arg0) {
*
* new DodajZakupy().execute(); }
*
* });
*/
/*
* Lzakupow = Lzakupow(); for (x = 0; x < Lzakupow.length; x++) { String
* temp = String.valueOf(Lzakupow[x]); // Toast.makeText(this, temp,
* Toast.LENGTH_SHORT).show(); Log.i("ddd", temp);
*/
}
public String getUsername() {
AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType("com.google");
List<String> possibleEmails = new LinkedList<String>();
for (Account account : accounts) {
// TODO: Check possibleEmail against an email regex or treat
// account.name as an email address only for certain account.type
// values.
possibleEmails.add(account.name);
}
if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
String email = possibleEmails.get(0);
String[] parts = email.split("@");
if (parts.length > 0 && parts[0] != null)
return parts[0];
else
return null;
} else
return null;
}
public void removeAtomPayOnClickHandler(View v) {
AtomPayment itemToRemove = (AtomPayment) v.getTag();
zakup[b] = itemToRemove.getName();
ile[b] = itemToRemove.getValue();
adapter.remove(itemToRemove);
b++;
}
private void setupListViewAdapter() {
adapter = new AtomPayListAdapter(Z_Glowna.this,
R.layout.atom_pay_list_item, new ArrayList<AtomPayment>());
ListView atomPaysListView = (ListView) findViewById(R.id.EnterPays_atomPaysList);
atomPaysListView.setAdapter(adapter);
}
public void setupAddPaymentButton() {
findViewById(R.id.EnterPays_addAtomPayment).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
adapter.insert(new AtomPayment("", 0), 0);
}
});
}
public class WczytajZakupy extends AsyncTask<Void, Void, Boolean> {
/*
* private ProgressDialog pDialog;
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Z_Glowna.this);
pDialog.setMessage("Synchrownizacja z chmurą...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(Void... arg0) {
// we will develop this method in version 2
mListaMarketow = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(WczytajZakupy);
try {
zakupy = json.getJSONArray(TAG_POSTS);
// looping through all posts according to the json object
// returned
for (int i = 0; i < zakupy.length(); i++) {
JSONObject c = zakupy.getJSONObject(i);
// gets the content of each tag
String market = c.getString(TAG_MARKET);
String users = c.getString(TAG_USERS);
String dlug = String.valueOf(c.getDouble((TAG_SZER)));
String id = String.valueOf(c.getDouble((TAG_ID)));
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_MARKET, market);
map.put(TAG_SZER, dlug);
map.put(TAG_USERS, users);
map.put(TAG_ID, id);
// adding HashList to ArrayList
mListaMarketow.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
try {
pDialog.dismiss();
pDialog = null;
} catch (Exception e) {
// nothing
}
int x = 0;
// Bundle extras = getIntent().getExtras();
String user = getUsername();
String users, idd; // zmienne do obsłużenia wyników z bazy danych
for (Map<String, String> mListaMar : mListaMarketow) {// pętla
// sprawdzająca
// ile dany
// użytkownik
// ma
// dokładnie
// pozycji
// zakupowych
// tworzy
// licznik
// który
// przechowuje
// zliczoną
// wartość
users = mListaMar.get(TAG_USERS);
if (users.equals(user)) {
y++;
}
}
Lzakupow = new double[y];
for (Map<String, String> mListaMar : mListaMarketow) {
idd = mListaMar.get(TAG_ID);
users = mListaMar.get(TAG_USERS);
longi = mListaMar.get(TAG_SZER);
name = mListaMar.get(TAG_MARKET);
double ilosc = Double.parseDouble(longi);
if (users.equals(user)) {
adapter.insert(new AtomPayment(name, ilosc), 0);
Lzakupow[x] = Double.parseDouble(idd);
x++;
}
}
ustawLzakupow(Lzakupow);
zakup = new String[y];
ile = new double[y];
}
}
public class DodajZakupy extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Z_Glowna.this);
pDialog.setMessage("Synchrownizacja z chmurą...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
// Retrieving Saved Username Data:
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(Z_Glowna.this);
String post_username = sp.getString("username", "anon");
int cos = adapter.getCount();
try {
for (int x = 0; x < cos; x++) {
// kowersja wszystkich danych na stringi
// il = x;
id = getUsername();
// id = String.valueOf(il);
nazwa = adapter.getItem(x).getName();
il = adapter.getItem(x).getValue();
ilosc = String.valueOf(il);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
params.add(new BasicNameValuePair("nazwa", nazwa));
params.add(new BasicNameValuePair("ilosc", ilosc));
Log.d("request!", "starting");
// Posting user data to script
json = jsonParser.makeHttpRequest(DodajZakupy, "POST",
params);
// full json response
Log.d("Post Comment attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
ustawJ(success);
}
// json success element
success = sprawdz();
if (success == 1) {
Log.d("Synchronizacja ukończona!", json.toString());
// finish();
return json.getString(TAG_MESSAGE);
} else {
Log.d("Błąd synchronizacji!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(Z_Glowna.this, file_url, Toast.LENGTH_LONG)
.show();
}
}
}
public class UsunZakupAsync extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Z_Glowna.this);
pDialog.setMessage("Usuwam wpisy...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
// Retrieving Saved Username Data:
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(Z_Glowna.this);
int x = 0;
Lzakupow = Lzakupow();
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (x = 0; x < Lzakupow.length; x++)
{
int idd = (int) Lzakupow[x];
String id = String.valueOf(idd);
Log.i("ddd", id);
params.add(new BasicNameValuePair("id", id));
JSONObject json = jsonParser.makeHttpRequest(UsunZakupy,
"POST", params);
/*try {
// Building Parameters
//List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
Log.d("request!", "starting");
// Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(UsunZakupy,
"POST", params);
// full json response
Log.d("Post Comment attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Comment Added!", json.toString());
finish();
return json.getString(TAG_MESSAGE);
} else {
Log.d("Comment Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}}*/
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(Z_Glowna.this, file_url, Toast.LENGTH_LONG)
.show();
}
}
}
public void ustawJ(int sprawdz) {
this.sprawdz = sprawdz;
}
public int sprawdz() {
return sprawdz;
}
public void ustawLzakupow(double[] Lzakupow) {
this.Lzakupow = Lzakupow;
}
public double[] Lzakupow() {
return Lzakupow;
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
// Toast.makeText(this, adapter.getItem(), duration)
/*int x = 0;
for (x = 0; x < zakup.length; x++) {
Toast.makeText(this, zakup[x], Toast.LENGTH_SHORT).show();
Toast.makeText(this, String.valueOf(ile[x]), Toast.LENGTH_SHORT)
.show();
x++;}*/
new DodajZakupy().execute();
new UsunZakupAsync().execute();
}
}
答案 0 :(得分:1)
这应该可以解答您的问题: java.lang.IllegalArgumentException: View not attached to window manager
我认为在你的情况下,这正是活动首先完成的问题。
答案 1 :(得分:0)
看起来创建一个Dialog会出现错误,你能放一些代码片段吗?当你创建一个对话框并且没有将它正确地附加到一个活动/它自己的布局时,这种错误就会出现......