android中AsyncTask启动Activity的问题

发布于 2024-12-17 20:57:45 字数 1872 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-24 20:57:45

我认为您正在从非 UI 线程使用 UI 线程(更新 UI 线程),这导致了您的问题。如果您想从非 UI 线程更新任何内容,您需要将代码放入 runOnUiThread() 中。

Activity_name.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // here you can add stuff to Update the UI.
            }
        });

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().

Activity_name.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // here you can add stuff to Update the UI.
            }
        });
养猫人 2024-12-24 20:57:45

如果没有完整的堆栈跟踪,很难确定,但我最好的猜测是您实际上得到了一个 ClassCastException

您的 ATMActivity 正在尝试将在 xml 中定义为 ImageButtonimageButton1 转换为 Button。这是不可能的,因为后者不是前者的超类。

// Can't do below: Button is not a superclass of ImageButton
Button btn = (Button) findViewById(R.id.imageButton1);

由于您是从 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 cast imageButton1, defined in xml as an ImageButton, to a Button. This is not possible, as the latter is not a superclass of the former.

// Can't do below: Button is not a superclass of ImageButton
Button btn = (Button) findViewById(R.id.imageButton1);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文