应用程序在动态加载大量UI布局时崩溃

时间:2013-05-07 14:41:07

标签: android user-interface

我正面临一个严重的问题。在我的主屏幕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中删除它现在就像魔术一样工作:) 顺便谢谢大家

4 个答案:

答案 0 :(得分:1)

让我建议你一些漂亮的编码实践,这样你就可以轻松地跟踪出现的问题:

首先,您在runOnUiThread内编写的代码可以很容易地重新考虑到一个方法,让我们说createDynamicUI,以便您的代码看起来像:

@Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {
        createDynamicUI();
        });
}

我没有看到从代码创建UI的意义;您可以使用XML完成此操作并且它根本不是动态的;据我所知,您的意思是您以这种方式动态放置组件;但是这与从静态编写XML一样,因为你看到你只是为每个组件编写代码;没有任何可重用的东西!如果你有一些随机的自动化过程可以创建一些相对的布局和位置,它将是动态的,值得用代码编写他们希望他们看到的地方。这样你甚至不必编写这个重量级代码来获取后台线程!

现在主要观点。 AsyncTask 用于执行一些大型处理工作,例如在您的UI休息时通过互联网获取数据而不会打扰它!所以你所做的已经打破了这个既定的秩序,并使整个系统无用! 您必须尝试的解决方案是:

  1. 使用XML来定义静态UI(到目前为止,它似乎来自您的 代码!)
  2. 分配AsyncTask数据提取工作及完成时间 更新您的UI。

  3. 同时您应该显示进度对话框 一些很酷的文字,这样你的用户就不会生气! :P

答案 1 :(得分:0)

你可以使用layoutInflater吗?并通过扩展View类以编程方式创建你的布局,并在你的主类的layoutInflater中使用该布局。

答案 2 :(得分:0)

 android:background="?android:attr/selectableItemBackground"

这是Android 2.2中的错误原因从XML中删除它现在就像魔术一样。

答案 3 :(得分:-1)

您需要了解AsyncTask的工作原理AsyncTask

您要在后台运行的所有计算或任何内容都应使用doInBackground方法完成。即使使用runOnUiThread(这是您的性能问题),也不要在此方法中执行与UI相关的任何操作

使用onPostExecute方法

更新您的用户界面