java - 单击 imageview 时 android 应用程序关闭且没有错误

标签 java android

我正在创建一个带有 id 的简单 ImageView ,并尝试在单击它时启动一个 Activity ,但是当我在模拟器中单击它时,应用程序就会崩溃并给出(等待或关闭应用程序提示) 这是我的:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">

<ImageView
    android:id="@+id/about"
    android:layout_width="match_parent"
    android:layout_height="450dp"
    android:background="@color/colorPrimaryDark"
    android:src="@drawable/twoth" />

</LinearLayout>

这是我的java代码

public class MainUi extends AppCompatActivity {

    //Variables Declaration.
    private Button btn1, btn2, btn3, btn4;
    private ImageView img;


    //Called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main_ui);
        //Variables Initialization and OnClick Method


        img = (ImageView) findViewById(R.id.about);
        img.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                //Start another activity
                Intent myIntent = new Intent(MainUi.this, Query.class);
                startActivity(myIntent);
            }
        });


    }


}

最佳答案

“等待或关闭”的事情让我认为查询 Activity 中有些东西在主线程上做了很多工作。

尝试使用 AsyncTask并将所有长时间运行的进程放入 doInBackground 部分,然后在 onPostExecute 中处理响应。

我想这很大程度上取决于您实际在做什么,但 AsyncTask 应该是一个好的开始。

关于java - 单击 imageview 时 android 应用程序关闭且没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43171419/

相关文章:

Android:从主屏幕小部件启动应用程序 Activity 的新实例

java - 如何在 Android 中检测暗照片

android - Google 登录按钮导致空对象引用

android - 想完全关闭应用程序

java - 媒体样式通知操作按钮不适用于 PendingIntent.getBroadcast()

java - 如何将数组列表中的特定值添加到 HashMap 中?

java - 如何确保Java类中的两个方法至少之一被重写?

java - 在 JTable 中显示图像

java - java枚举数据类型中的构造函数

java - 操作栏 : Center text on navigation bar buttons