我目前正在开发一个应用程序,用户可以在其中将日志发布到项目中。但是,好像我遇到了一个问题,似乎无法找到答案。用户发布帖子后,后退按钮不会关闭日志 Activity 。相反,它会使应用程序无响应;来自此类实例的 logcat 位于底部。但是,单击“等待”而不是“强制关闭”会恢复应用程序,但后退按钮仍然不起作用。下面是 Activity 的布局。 viewflipper 中的每个 View 都是项目的不同日志,其中包含多个用于填写信息的编辑文本和一个用于发布到日志的按钮。最初,这是在当前帖子显示在 ListView 中的帖子按钮下方的位置进行布局的。那时,后退按钮具有全部功能。但是, ScrollView 中的 ListView 效果不佳,因此必须将其更改为带有 ListView 的滑动抽屉,每次用户在日志之间切换时,其适配器都会更改。这是后退按钮功能丢失的地方。起初我以为我的 onKeyDown 方法做错了,但在尝试调试、修改甚至删除它之后,我发现它从未被调用过。因为这个是我换了抽屉后开始出现的,所以我的结论是和抽屉有关系。对此的任何帮助将不胜感激。
编辑: 在深入研究这个问题后,我发现不仅仅是后退按钮会造成这种情况。设置按钮和搜索按钮也都表现出这种行为。唯一显示正确功能的按钮是主页按钮。但是, Activity 中的功能不受影响。我仍然可以发帖、在 View 之间切换以及查看和编辑帖子。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/iphonebg" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@drawable/toolbar_gradient"
android:gravity="center_vertical" >
<!-- Header -->
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" >
<!-- Date -->
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" >
<!-- Tabs -->
</TableRow>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/iphonebg" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ViewFlipper
android:id="@+id/ViewFlipper01"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="2" >
<!-- adding views to ViewFlipper -->
<!-- ScrollView 1 -->
<!-- ScrollView 2 -->
<!-- ScrollView 3 -->
<!-- ScrollView 4 -->
<!-- ScrollView 5 -->
</ViewFlipper>
</TableRow>
</TableLayout>
<SlidingDrawer
android:id="@+id/drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:content="@+id/anyPosts"
android:handle="@+id/handle" >
<Button
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="35dip"
android:background="@drawable/black_btn_bg"
android:text="Drag or Tap to View Posts"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<ListView
android:id="@+id/anyPosts"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5dip"
android:paddingRight="5dip" >
</ListView>
</SlidingDrawer>
</FrameLayout>
</TableLayout>
处理滑动抽屉的代码范围,位于我的 onCreate 方法中:
final SlidingDrawer slide = (SlidingDrawer) findViewById(R.id.drawer);
final Button btnDownloads = (Button) findViewById(R.id.handle);
slide.setOnDrawerOpenListener(new OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
btnDownloads.setText("Drag or Tap to Close Posts");
}
});
slide.setOnDrawerCloseListener(new OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
btnDownloads.setText("Drag or Tap to View Posts");
}
});
slide.animateOpen();
处理 ListView 的代码范围:
ListView listToFill = (ListView) findViewById(R.id.anyPosts);
listToFill.setAdapter(listViewAdapter);
日志:
06-07 13:51:44.394: I/InputDispatcher(1464): Application is not responding: AppWindowToken{40cd7758 token=HistoryRecord{40534788 uda.projectlogging/.Radio_Group}} - Window{40ae0a88 uda.projectlogging/uda.projectlogging.Radio_Group paused=false}. 10009.8ms since event, 10009.4ms since wait started
06-07 13:51:44.394: I/WindowManager(1464): Input event dispatching timed out sending to uda.projectlogging/uda.projectlogging.Radio_Group
06-07 13:51:44.444: I/Process(1464): Sending signal. PID: 23939 SIG: 3
06-07 13:51:44.444: I/dalvikvm(23939): threadid=4: reacting to signal 3
06-07 13:51:44.444: I/dalvikvm(23939): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.444: I/Process(1464): Sending signal. PID: 1464 SIG: 3
06-07 13:51:44.444: I/dalvikvm(1464): threadid=4: reacting to signal 3
06-07 13:51:44.484: I/dalvikvm(1464): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.484: I/Process(1464): Sending signal. PID: 1690 SIG: 3
06-07 13:51:44.484: I/dalvikvm(1690): threadid=4: reacting to signal 3
06-07 13:51:44.484: I/dalvikvm(1690): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.484: I/Process(1464): Sending signal. PID: 1639 SIG: 3
06-07 13:51:44.484: I/dalvikvm(1639): threadid=4: reacting to signal 3
06-07 13:51:44.494: I/dalvikvm(1639): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.494: I/Process(1464): Sending signal. PID: 1689 SIG: 3
06-07 13:51:44.494: I/dalvikvm(1689): threadid=4: reacting to signal 3
06-07 13:51:44.494: I/dalvikvm(1689): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.494: I/Process(1464): Sending signal. PID: 1672 SIG: 3
06-07 13:51:44.494: I/dalvikvm(1672): threadid=4: reacting to signal 3
06-07 13:51:44.494: I/dalvikvm(1672): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.494: I/Process(1464): Sending signal. PID: 1656 SIG: 3
06-07 13:51:44.494: I/dalvikvm(1656): threadid=4: reacting to signal 3
06-07 13:51:44.504: I/dalvikvm(1656): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.504: I/Process(1464): Sending signal. PID: 1652 SIG: 3
06-07 13:51:44.504: I/dalvikvm(1652): threadid=4: reacting to signal 3
06-07 13:51:44.514: I/dalvikvm(1652): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.514: I/Process(1464): Sending signal. PID: 1642 SIG: 3
06-07 13:51:44.514: I/dalvikvm(1642): threadid=4: reacting to signal 3
06-07 13:51:44.514: I/dalvikvm(1642): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.514: I/Process(1464): Sending signal. PID: 1664 SIG: 3
06-07 13:51:44.514: I/dalvikvm(1664): threadid=4: reacting to signal 3
06-07 13:51:44.514: I/dalvikvm(1664): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.514: I/Process(1464): Sending signal. PID: 1675 SIG: 3
06-07 13:51:44.514: I/dalvikvm(1675): threadid=4: reacting to signal 3
06-07 13:51:44.514: I/dalvikvm(1675): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.514: I/Process(1464): Sending signal. PID: 1691 SIG: 3
06-07 13:51:44.514: I/dalvikvm(1691): threadid=4: reacting to signal 3
06-07 13:51:44.514: I/dalvikvm(1691): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.514: I/Process(1464): Sending signal. PID: 1590 SIG: 3
06-07 13:51:44.514: I/dalvikvm(1590): threadid=4: reacting to signal 3
06-07 13:51:44.524: I/dalvikvm(1590): Wrote stack traces to '/data/anr/traces.txt'
06-07 13:51:44.734: D/dalvikvm(1464): GC_CONCURRENT freed 1754K, 33% free 9128K/13511K, external 6035K/6964K, paused 2ms+6ms
06-07 13:51:44.814: D/dalvikvm(1464): GC_EXPLICIT freed 31K, 33% free 9107K/13511K, external 6035K/6964K, paused 71ms
06-07 13:51:44.914: E/NetlinkEvent(1351): NetlinkEvent::FindParam(): Parameter 'UDEV_LOG' not found
06-07 13:51:45.364: E/ActivityManager(1464): ANR in uda.projectlogging, pid 23939 (uda.projectlogging/.Radio_Group)
06-07 13:51:45.364: E/ActivityManager(1464): Reason: keyDispatchingTimedOut
06-07 13:51:45.364: E/ActivityManager(1464): Load: 0.26 / 0.25 / 0.21
06-07 13:51:45.364: E/ActivityManager(1464): CPU usage from 7340ms to 0ms ago with 99% awake:
06-07 13:51:45.364: E/ActivityManager(1464): 0.9% 783/als_wq: 0% user + 0.9% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 0.8% 1464/system_server: 0.1% user + 0.6% kernel / faults: 1 minor
06-07 13:51:45.364: E/ActivityManager(1464): 0.5% 1489/ActivityManager: 0.1% user + 0.4% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 0.2% 1543/InputDispatcher: 0% user + 0.2% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 0.1% 1539/PowerManagerSer: 0% user + 0.1% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 0% 19639/com.svox.pico: 0% user + 0% kernel / faults: 56 minor
06-07 13:51:45.364: E/ActivityManager(1464): 0% 19639/com.svox.pico: 0% user + 0% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 0.4% 288/tegra_spi.1: 0% user + 0.4% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 0.4% 19564/com.android.vending: 0.2% user + 0.1% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 0.2% 19566/HeapWorker: 0.1% user + 0.1% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 0% 1358/battd: 0% user + 0% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 0.1% 21254/com.motorola.android.datamanager: 0.1% user + 0% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 0.1% 21255/HeapWorker: 0.1% user + 0% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 7.9% TOTAL: 1.3% user + 2% kernel + 0.1% iowait + 4.3% softirq
06-07 13:51:45.364: E/ActivityManager(1464): CPU usage from 420ms to 934ms later with 99% awake:
06-07 13:51:45.364: E/ActivityManager(1464): 7.6% 1464/system_server: 1.9% user + 5.7% kernel / faults: 1 minor
06-07 13:51:45.364: E/ActivityManager(1464): 3.8% 1543/InputDispatcher: 0% user + 3.8% kernel
06-07 13:51:45.364: E/ActivityManager(1464): 3.9% TOTAL: 1.9% user + 1.9% kernel
当点击发布按钮时:
postButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.image_click));
data = new JSONObject();
if (description.equals("")) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"Please Enter a Description",
Toast.LENGTH_LONG).show();
}
});
} else if (delayStart == null) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"Please Set Delay Duration",
Toast.LENGTH_LONG).show();
}
});
} else {
try {
data.put("COMMENT", description);
data.put("DELAYSTART",
delayStart.get(Calendar.HOUR_OF_DAY) + ":"
+ delayStart.get(Calendar.MINUTE));
data.put("DELAYEND", delayEnd.get(Calendar.HOUR_OF_DAY)
+ ":" + delayEnd.get(Calendar.MINUTE));
postEntry();
description = "";
delayHours = 0;
delayMinutes = 0;
delayStart = null;
delayEnd = null;
delay.setText("");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
发布条目:
public void postEntry() {
final ProgressDialog dialog1 = new ProgressDialog(this);
dialog1.setCancelable(true);
dialog1.setMessage("Loading...");
dialog1.show();
final Thread background1 = new Thread(new Runnable() {
public void run() {
Looper.prepare();
try {
String type1 = "0";
switch (current) {
case 0:
type1 = "1";
break;
case 1:
type1 = "4";
break;
case 2:
type1 = "3";
break;
case 3:
type1 = "6";
break;
case 4:
type1 = "5";
break;
}
if (helperMethods.isOnline(currentActivity)) {
if (failedUploads)
doFailedUploads();
SoapObject request = new SoapObject(
"http://www.constructiononline.com/service/",
"OSLPost");
PropertyInfo num1 = new PropertyInfo();
num1.setName("userEmail");
num1.setValue(settings.getString("EMAIL", ""));
request.addProperty(num1);
num1 = new PropertyInfo();
num1.setName("prj_id");
num1.setValue(projectInfo.getID());
request.addProperty(num1);
num1 = new PropertyInfo();
num1.setName("entryDate");
num1.setValue(month + "/" + day + "/" + year);
request.addProperty(num1);
num1 = new PropertyInfo();
num1.setName("type");
num1.setValue(type1);
request.addProperty(num1);
num1 = new PropertyInfo();
num1.setName("data");
num1.setValue(data.toString());
request.addProperty(num1);
num1 = new PropertyInfo();
num1.setName("delete");
num1.setValue("false");
request.addProperty(num1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(
URL);
androidHttpTransport
.call("http://www.constructiononline.com/service/OSLPost",
envelope);
Log.e("Response from Post", envelope.getResponse()
.toString());
updatePostList(false);
} else {
addFailedUploads(settings.getString("EMAIL", ""),
projectInfo.getID(), month + "/" + day + "/"
+ year, type1, data.toString());
JSONObject j = new JSONObject();
j.put("LOG_OBJECT", data);
j.put("TYPE", type1);
j.put("DATE", month + "/" + day + "/" + year);
Calendar c = Calendar.getInstance();
String created = c.get(Calendar.MONTH) + "/"
+ c.get(Calendar.DAY_OF_MONTH) + "/"
+ c.get(Calendar.YEAR) + " "
+ c.get(Calendar.HOUR) + ":"
+ c.get(Calendar.MINUTE) + ":"
+ c.get(Calendar.SECOND) + " "
+ (c.get(Calendar.AM_PM) == 0 ? "AM" : "PM");
j.put("CREATED", created);
JSONObject creatorTemp = new JSONObject();
creatorTemp.put("ID", "0");
creatorTemp.put("NAME", " ");
creatorTemp.put("COMPANY", " ");
creatorTemp.put("ICON", "NULL");
j.put("CREATOR", creatorTemp);
j.put("ID", "0");
switch (current) {
case 0:
weatherEntries.put(j);
break;
case 1:
workEntries.put(j);
break;
case 2:
visitorEntries.put(j);
break;
case 3:
deliveryEntries.put(j);
break;
case 4:
noteEntries.put(j);
break;
}
}
if (dialog1 != null && dialog1.isShowing()) {
dialog1.dismiss();
}
} catch (final Exception e) {
e.printStackTrace();
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
addOnlineErrorLogEntry(e.getMessage(), stacktrace);
if (dialog1 != null && dialog1.isShowing()) {
dialog1.dismiss();
}
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
e.getMessage() + " in setupProjects",
Toast.LENGTH_LONG).show();
}
});
}
}
});
// start the background thread
background1.start();
}
更新帖子列表:
private void updatePostList(final boolean getAll) {
if (!helperMethods.isOnline(currentActivity)) {
return;
}
final ProgressDialog dialog1 = new ProgressDialog(this);
dialog1.setCancelable(true);
dialog1.setMessage("Loading...");
dialog1.show();
final Thread background1 = new Thread(new Runnable() {
public void run() {
Looper.prepare();
try {
if (failedUploads)
doFailedUploads();
SoapObject request = new SoapObject(
"http://www.constructiononline.com/service/",
"OSLGet");
PropertyInfo num1 = new PropertyInfo();
num1.setName("userEmail");
num1.setValue(settings.getString("EMAIL", ""));
request.addProperty(num1);
num1 = new PropertyInfo();
num1.setName("prj_id");
num1.setValue(projectInfo.getID());
request.addProperty(num1);
num1 = new PropertyInfo();
num1.setName("startDate");
num1.setValue("");
if (!getAll) {
num1.setValue(month + "/" + day + "/" + year);
}
request.addProperty(num1);
num1 = new PropertyInfo();
num1.setName("endDate");
num1.setValue("");
request.addProperty(num1);
num1 = new PropertyInfo();
num1.setName("type");
num1.setValue("");
if (!getAll) {
switch (current) {
case 0:
num1.setValue("1");
break;
case 1:
num1.setValue("4");
break;
case 2:
num1.setValue("3");
break;
case 3:
num1.setValue("6");
break;
case 4:
num1.setValue("5");
break;
}
}
request.addProperty(num1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(
URL);
androidHttpTransport.call(
"http://www.constructiononline.com/service/OSLGet",
envelope);
Log.e("response", envelope.getResponse().toString());
JSONObject temp = new JSONObject(envelope.getResponse()
.toString());
JSONArray entries = temp.getJSONArray("ENTRIES");
if (getAll) {
weatherEntries = new JSONArray();
workEntries = new JSONArray();
visitorEntries = new JSONArray();
deliveryEntries = new JSONArray();
noteEntries = new JSONArray();
}
switch (current) {
case 0:
weatherEntries = new JSONArray();
break;
case 1:
workEntries = new JSONArray();
break;
case 2:
visitorEntries = new JSONArray();
break;
case 3:
deliveryEntries = new JSONArray();
break;
case 4:
noteEntries = new JSONArray();
break;
}
for (int x = 0; x < entries.length(); x++) {
switch (entries.getJSONObject(x).getInt("TYPE")) {
case 1:
weatherEntries.put(entries.getJSONObject(x));
break;
case 3:
visitorEntries.put(entries.getJSONObject(x));
break;
case 4:
workEntries.put(entries.getJSONObject(x));
break;
case 5:
noteEntries.put(entries.getJSONObject(x));
break;
case 6:
deliveryEntries.put(entries.getJSONObject(x));
break;
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
if (getAll) {
setupWeatherLog();
} else {
getPosts();
}
}
});
if (dialog1 != null && dialog1.isShowing()) {
dialog1.dismiss();
}
} catch (final Exception e) {
e.printStackTrace();
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
addOnlineErrorLogEntry(e.getMessage(), stacktrace);
if (dialog1 != null && dialog1.isShowing()) {
dialog1.dismiss();
}
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
e.getMessage() + " in setupProjects",
Toast.LENGTH_LONG).show();
}
});
}
if (dialog1 != null && dialog1.isShowing()) {
dialog1.dismiss();
}
}
});
// start the background thread
background1.start();
}
填充 ListView :
private void getPosts() {
JSONArray entriesToShow = new JSONArray();
JSONArray postsToLoad = null;
switch (current) {
case 0:
postsToLoad = weatherEntries;
break;
case 1:
postsToLoad = workEntries;
break;
case 2:
postsToLoad = visitorEntries;
break;
case 3:
postsToLoad = deliveryEntries;
break;
case 4:
postsToLoad = noteEntries;
break;
}
for (int i = 0; i < postsToLoad.length(); i++) {
try {
JSONObject temp = postsToLoad.getJSONObject(i);
String[] tempDay = temp.getString("DATE").split("/");
if (Integer.parseInt(tempDay[0]) == month
&& Integer.parseInt(tempDay[1]) == day
&& Integer.parseInt(tempDay[2]) == year) {
entriesToShow.put(temp);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
BaseAdapter listViewAdapter;
switch (current) {
case 0:
listViewAdapter = new WeatherDeliveryVisitLazyAdapter(
currentActivity, entriesToShow, current);
break;
case 1:
listViewAdapter = new WorkLogPostLazyAdapter(currentActivity,
entriesToShow);
break;
case 2:
listViewAdapter = new WeatherDeliveryVisitLazyAdapter(
currentActivity, entriesToShow, current);
break;
case 3:
listViewAdapter = new WeatherDeliveryVisitLazyAdapter(
currentActivity, entriesToShow, current);
break;
case 4:
listViewAdapter = new ProjectNotePostLazyAdapter(currentActivity,
entriesToShow);
break;
default:
listViewAdapter = new ProjectNotePostLazyAdapter(currentActivity,
entriesToShow);
}
if (listViewAdapter == null || listViewAdapter.getCount() == 0) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"No Posts to Display", Toast.LENGTH_SHORT).show();
}
});
}
ListView listToFill = (ListView) findViewById(R.id.anyPosts);
listToFill.setAdapter(listViewAdapter);
}
这就是在任何日志中单击发布按钮时它所做的一切。如果我正确地解释了这一点,当所有进度对话框都清除了屏幕时,后台线程已经结束,并且在出现错误或信息不完整的情况下,除了 toast 之外,唯一在 ui 线程上运行的是设置 ListView ,它确实完成了。当我拉开抽屉时,它会显示正确的帖子列表。
编辑 我发现如果我在 updatePostList 方法中注释掉进度对话框,它就可以正常工作。如此新的问题:到底为什么这是解决方案?
最终编辑 我想通了。该进度对话框是从后台线程启动的。修改了 postEntry 方法,使其在 UI 线程上调用 updatePostList,以便在 UI 线程上创建进度对话框,并且可以正常工作。是的。
最佳答案
根据您的描述,您可能正在从主线程启动一些长时间运行的进程。在主线程上执行诸如写入文件、更新数据库或执行网络 I/O 之类的操作将导致它变得无响应。执行此类操作时,您应该使用 Service
或 AsyncTask
。
以下是一些可能对您有用的文章:
关于android - 为什么 android 按钮会卡住我的应用程序并且无法运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10938733/