我正在创建一个带有 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/