我面临一个严重的问题。在我的主屏幕 UI 中,活动首先必须确定屏幕大小,然后必须根据该数据动态添加 GUI 元素。准确地说,我在一个父布局下使用了 13 个子相对布局。因此,您可以了解动态确定它们的大小并在它们上加载图像需要一些时间。在高性能设备(Galaxy S3)中,加载数据大约需要 3 秒,但在低性能设备中,应用程序崩溃。我尝试使用 asynctask,但同样的事情发生了。如果有人可以在这件事上提供帮助,那将非常有帮助。代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
pd = new ProgressDialog(this);
asyncTask = new LoadHome();
asyncTask.execute();
}
private class LoadHome extends AsyncTask<String, Integer, String> implements
OnClickListener {
@Override
protected void onPreExecute() {
super.onPreExecute();
// initialize the dialog
pd.setTitle("Loading...");
pd.setMessage("Please wait while loading...");
pd.setIndeterminate(true);
pd.setCancelable(true);
pd.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
setContentView(R.layout.activity_home);
Display display = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
int width = display.getWidth();
@SuppressWarnings("deprecation")
int height = display.getHeight();
pal = (RelativeLayout) findViewById(R.id.item1);
pa = new RelativeLayout.LayoutParams((int) (width * 0.625),
(int) (height * 0.1876));
pa.addRule(RelativeLayout.BELOW, R.id.itemBC);
pa.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
pa.addRule(RelativeLayout.LEFT_OF, R.id.item2);
pal.setLayoutParams(pa);
bdnews = (RelativeLayout) findViewById(R.id.item9);
bdn = new RelativeLayout.LayoutParams(
(int) (width * 0.3125), (int) (height * 0.1876));
bdn.addRule(RelativeLayout.RIGHT_OF, R.id.item8);
bdn.addRule(RelativeLayout.BELOW, R.id.item6);
bdnews.setLayoutParams(bdn);
ds = (RelativeLayout) findViewById(R.id.item6);
star = new RelativeLayout.LayoutParams(
(int) (width * 0.625), (int) (height * 0.1876));
star.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
star.addRule(RelativeLayout.BELOW, R.id.item4);
ds.setLayoutParams(star);
somokal = (RelativeLayout) findViewById(R.id.item4);
sk = new RelativeLayout.LayoutParams(
(int) (width * 0.3125), (int) (height * 0.1876));
sk.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
sk.addRule(RelativeLayout.BELOW, R.id.item3);
somokal.setLayoutParams(sk);
fe = (RelativeLayout) findViewById(R.id.item3);
fb = new RelativeLayout.LayoutParams(
(int) (width * 0.3125), (int) (height * 0.1876));
fb.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
fb.addRule(RelativeLayout.BELOW, R.id.item1);
fe.setLayoutParams(fb);
banglanews = (RelativeLayout) findViewById(R.id.item7);
ban = new RelativeLayout.LayoutParams(
(int) (width * 0.3125), (int) (height * 0.3752));
ban.addRule(RelativeLayout.BELOW, R.id.item5);
ban.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ban.addRule(RelativeLayout.RIGHT_OF, R.id.item6);
banglanews.setLayoutParams(ban);
adesh = (RelativeLayout) findViewById(R.id.item2);
amard = new RelativeLayout.LayoutParams(
(int) (width * 0.3125), (int) (height * 0.1876));
amard.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
amard.addRule(RelativeLayout.BELOW, R.id.itemBC);
adesh.setLayoutParams(amard);
kk = (RelativeLayout) findViewById(R.id.item8);
kko = new RelativeLayout.LayoutParams(
(int) (width * 0.3125), (int) (height * 0.1876));
kko.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
kko.addRule(RelativeLayout.BELOW, R.id.item6);
kk.setLayoutParams(kko);
bbcn = (RelativeLayout) findViewById(R.id.item5);
bbc = new RelativeLayout.LayoutParams(
(int) (width * 0.625), (int) (height * 0.3752));
bbc.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
bbc.addRule(RelativeLayout.BELOW, R.id.item2);
bbc.addRule(RelativeLayout.RIGHT_OF, R.id.item3);
bbcn.setLayoutParams(bbc);
independent = (RelativeLayout) findViewById(R.id.item10);
ind = new RelativeLayout.LayoutParams(
(int) (width * 0.625), (int) (height * 0.1876));
ind.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ind.addRule(RelativeLayout.BELOW, R.id.item8);
independent.setLayoutParams(ind);
vorerkagoj = (RelativeLayout) findViewById(R.id.item11);
vor = new RelativeLayout.LayoutParams(
(int) (width * 0.3125), (int) (height * 0.1876));
vor.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
vor.addRule(RelativeLayout.BELOW, R.id.item7);
vor.addRule(RelativeLayout.RIGHT_OF, R.id.item10);
vorerkagoj.setLayoutParams(vor);
ittefaq = (RelativeLayout) findViewById(R.id.item12);
iff = new RelativeLayout.LayoutParams(
(int) (width * 0.3125), (int) (height * 0.1876));
iff.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
iff.addRule(RelativeLayout.BELOW, R.id.item10);
ittefaq.setLayoutParams(iff);
jonokontho = (RelativeLayout) findViewById(R.id.item13);
jono = new RelativeLayout.LayoutParams(
(int) (width * 0.625), (int) (height * 0.1876));
jono.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
jono.addRule(RelativeLayout.BELOW, R.id.item11);
jono.addRule(RelativeLayout.RIGHT_OF, R.id.item12);
jonokontho.setLayoutParams(jono);
btn0 = (ImageButton) findViewById(R.id.paloimg);
btn0.setImageResource(R.drawable.palo);
btn1 = (ImageButton) findViewById(R.id.adimg);
btn1.setImageResource(R.drawable.amardesh);
btn2 = (ImageButton) findViewById(R.id.feimg);
btn2.setImageResource(R.drawable.fe);
btn3 = (ImageButton) findViewById(R.id.sking);
btn3.setImageResource(R.drawable.somokal);
btn4 = (ImageButton) findViewById(R.id.bbcimg);
btn4.setImageResource(R.drawable.bbc);
btn5 = (ImageButton) findViewById(R.id.dsimg);
btn5.setImageResource(R.drawable.dsbg);
btn6 = (ImageButton) findViewById(R.id.kkimg);
btn6.setImageResource(R.drawable.kk);
btn7 = (ImageButton) findViewById(R.id.bdimg);
btn7.setImageResource(R.drawable.bdnew);
btn8 = (ImageButton) findViewById(R.id.banglaimg);
btn8.setImageResource(R.drawable.bangla);
btn9 = (ImageButton) findViewById(R.id.indimg);
btn9.setImageResource(R.drawable.independent);
btn10 = (ImageButton) findViewById(R.id.bdvorerk);
btn10.setImageResource(R.drawable.vk);
btn11 = (ImageButton) findViewById(R.id.bdiff);
btn11.setImageResource(R.drawable.iff);
btn12 = (ImageButton) findViewById(R.id.bdjk);
btn12.setImageResource(R.drawable.jonokontho);
infoBtn = (ImageButton) findViewById(R.id.aboutBtn);
}
});
return null;
}
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pd.dismiss();
// OnClickListener allocation
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn3.setOnClickListener(this);
btn2.setOnClickListener(this);
btn9.setOnClickListener(this);
btn10.setOnClickListener(this);
btn11.setOnClickListener(this);
btn12.setOnClickListener(this);
infoBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setCancelable(true);
//builder.setIcon(R.drawable.dev);
builder.setTitle("Developer Info");
builder.setInverseBackgroundForced(true);
String Text1 = "News flash is developed by";
String Text2 = "TEAM ORANGE JUICE";
String Text3 = "email: teamoj@gmail.com";
builder.setMessage(Text1 +"\n"+ Text2 +"\n"+ Text3);
builder.setPositiveButton("Close",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
AlertDialog alert=builder.create();
alert.show();
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
switch (id) {
//code goes here which are not included...
}
}
}
日志:
05-04 21:23:41.601: I/ApplicationPackageManager(17393): cscCountry is not German : TML
05-04 21:23:41.773: D/AndroidRuntime(17393): Shutting down VM
05-04 21:23:41.773: W/dalvikvm(17393): threadid=1: thread exiting with uncaught exception (group=0x40018578)
05-04 21:23:41.796: E/AndroidRuntime(17393): FATAL EXCEPTION: main
05-04 21:23:41.796: E/AndroidRuntime(17393): android.view.InflateException: Binary XML file line #44: Error inflating class android.widget.ImageButton
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
05-04 21:23:41.796: E/AndroidRuntime(17393): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-04 21:23:41.796: E/AndroidRuntime(17393): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.app.Activity.setContentView(Activity.java:1657)
05-04 21:23:41.796: E/AndroidRuntime(17393): at com.example.decoupled.HomeActivity$LoadHome$1.run(HomeActivity.java:76)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.os.Handler.handleCallback(Handler.java:587)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.os.Handler.dispatchMessage(Handler.java:92)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.os.Looper.loop(Looper.java:130)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-04 21:23:41.796: E/AndroidRuntime(17393): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 21:23:41.796: E/AndroidRuntime(17393): at java.lang.reflect.Method.invoke(Method.java:507)
05-04 21:23:41.796: E/AndroidRuntime(17393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-04 21:23:41.796: E/AndroidRuntime(17393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-04 21:23:41.796: E/AndroidRuntime(17393): at dalvik.system.NativeStart.main(Native Method)
05-04 21:23:41.796: E/AndroidRuntime(17393): Caused by: java.lang.reflect.InvocationTargetException
05-04 21:23:41.796: E/AndroidRuntime(17393): at java.lang.reflect.Constructor.constructNative(Native Method)
05-04 21:23:41.796: E/AndroidRuntime(17393): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
05-04 21:23:41.796: E/AndroidRuntime(17393): ... 20 more
05-04 21:23:41.796: E/AndroidRuntime(17393): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x101030e a=-1}
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.content.res.Resources.loadDrawable(Resources.java:1681)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.view.View.<init>(View.java:1951)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.widget.ImageView.<init>(ImageView.java:112)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.widget.ImageButton.<init>(ImageButton.java:85)
05-04 21:23:41.796: E/AndroidRuntime(17393): at android.widget.ImageButton.<init>(ImageButton.java:81)
05-04 21:23:41.796: E/AndroidRuntime(17393): ... 23 more
回答:
android:background="?android:attr/selectableItemBackground"
这就是 android 2.2 中错误的原因从 XML 中删除了它,它现在像魔术一样工作:) 顺便谢谢大家