android中AsyncTask启动Activity的问题
我在 Android 应用程序中的 AsyncTask
中遇到一个问题。问题是,我正在我的应用程序中从 AsyncTask
启动另一个 Activity
,当其布局具有简单的 Button
时,该活动运行良好,但是当我使用ImageButton
它给了我处理程序和 Looper.loop 的错误。我不明白发生这种错误的原因。我在调用 Activity
时显示带有图像的菜单。
有人可以建议我这有什么问题以及如何实现这种功能吗?
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (Dialog != null && Dialog.isShowing()) {
Dialog.dismiss();
locationManager.removeUpdates(locationListener);
Intent homeIntent = new Intent(ATMActivity.this.getApplicationContext(), HomeMenuActivity.class);
homeIntent.putExtra("lat", latitude);
homeIntent.putExtra("lng", longitude);
startActivity(homeIntent);
}
}
XML 文件:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/atm_icon"
android:onClick="buttonClicked" />
</LinearLayout>
另一个活动类:-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button btn = (Button) findViewById(R.id.imageButton1);
btn.setHint("ATM");
float latitude = getIntent().getFloatExtra("lat", 0);
float longitude = getIntent().getFloatExtra("lng", 0);
Toast.makeText(getApplicationContext(), "Location Floats:- " + latitude + "," + longitude, Toast.LENGTH_LONG).show();
}
I am facing one problem in AsyncTask
in my application in Android. The problem is, I am starting another Activity
from AsyncTask
in my application which runs fine when its layout has simple Button
, but when I am using ImageButton
its giving me error of handler and looper.loop. I am not getting the reason why this kind of error is occurring. I am displaying menus with images in calling Activity
.
Can anybody suggest me what is the problem in this and how can I achieve this kind of functionality?
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (Dialog != null && Dialog.isShowing()) {
Dialog.dismiss();
locationManager.removeUpdates(locationListener);
Intent homeIntent = new Intent(ATMActivity.this.getApplicationContext(), HomeMenuActivity.class);
homeIntent.putExtra("lat", latitude);
homeIntent.putExtra("lng", longitude);
startActivity(homeIntent);
}
}
XML File :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/atm_icon"
android:onClick="buttonClicked" />
</LinearLayout>
Another Activity Class :-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button btn = (Button) findViewById(R.id.imageButton1);
btn.setHint("ATM");
float latitude = getIntent().getFloatExtra("lat", 0);
float longitude = getIntent().getFloatExtra("lng", 0);
Toast.makeText(getApplicationContext(), "Location Floats:- " + latitude + "," + longitude, Toast.LENGTH_LONG).show();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在从非 UI 线程使用 UI 线程(更新 UI 线程),这导致了您的问题。如果您想从非 UI 线程更新任何内容,您需要将代码放入
runOnUiThread()
中。I think you are playing with UI thread(updating UI thread) from a non-UI thread, that is causing you the problem. If you want to update anything from the non-UI thread you need to put the code inside the
runOnUiThread()
.如果没有完整的堆栈跟踪,很难确定,但我最好的猜测是您实际上得到了一个
ClassCastException
。您的
ATMActivity
正在尝试将在 xml 中定义为ImageButton
的imageButton1
转换为Button
。这是不可能的,因为后者不是前者的超类。由于您是从 AsyncTask 的 onPostExecute 启动活动的,因此您可能会在堆栈跟踪中发现一些提到循环器/处理程序的内容。
Without the full stack trace it's hard to say for sure, but my best guess is you're actually getting a
ClassCastException
.Your
ATMActivity
is trying to castimageButton1
, defined in xml as anImageButton
, to aButton
. This is not possible, as the latter is not a superclass of the former.Since you're starting the activity from the onPostExecute of an AsyncTask, you'll probably find some stuff mentioning a looper/handler in the stack trace.