我正在创建一个带有id的简单imageview并尝试在点击它时启动一个活动但是当我在模拟器中点击它时,应用程序崩溃并给我(等待或关闭应用提示) 这是我的:
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);
}
});
}
}
答案 0 :(得分:2)
等待或关闭&#39;这件事让我觉得在Query活动中有一些东西在主线程上做了很多工作。
尝试使用AsyncTask并将所有长时间运行的进程放在doInBackground部分中,然后在onPostExecute中处理响应。
我想这很大程度上取决于你实际在做什么,但是AsyncTask应该是一个好的开始。
答案 1 :(得分:0)
这里是完整的代码,我添加了其余的代码,以防您可以从外部(Query)类处理它,而不是在Async Class中。
public class Query extends AppCompatActivity {
private ArrayList<String[]> BB;
private ArrayList<String[]> AA = new ArrayList<String[]>();
private Socket socket = null;
private ObjectInputStream in = null;
private DataOutputStream out = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
try {
AA = new AsyncAction().execute().get();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
setTextViews();
}
}
public void setTextViews() {
View linearLayout = findViewById(R.id.info);
for (int y = 0; y < AA.size(); y++) {
// for (int x = 0; x < AA.get(0).length; x++) {}
TextView name = new TextView(this);
name.setText(AA.get(y)[0]);
name.setId(y);
name.setGravity(Gravity.CENTER);
name.setTextSize(30);
name.setPadding(0, 30, 0, 0);
name.setTextColor(this.getResources().getColor(R.color.icons));
name.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
ProgressBar pb = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
Drawable drawable = pb.getProgressDrawable();
drawable.setColorFilter(new LightingColorFilter(0xFF000000, this.getResources().getColor(R.color.colorPrimary)));
pb.setProgress(100);
final int finalY = y;
name.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Bundle b = new Bundle();
b.putStringArray("list", AA.get(finalY));
Intent i = new Intent(Query.this, Info.class);
i.putExtras(b);
startActivity(i);
}
});
((LinearLayout) linearLayout).addView(name);
((LinearLayout) linearLayout).addView(pb);
}
}
private class AsyncAction extends AsyncTask<String, Void, ArrayList> {
protected ArrayList doInBackground(String... args) {
try {
socket = new Socket(port, 8888);
out = new DataOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
// action
out.writeInt(2);
try {
in = new ObjectInputStream(socket.getInputStream());
BB = (ArrayList<String[]>) in.readObject();
in.close();
socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return BB;//returns what you want to pass to the onPostExecute()
}
protected void onPostExecute(ArrayList result) {
AA = result;
}
}
}