0

我面临一个严重的问题。在我的主屏幕 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

4 回答 4

1

让我向您推荐一些漂亮的编码实践,以便您可以轻松跟踪问题所在:

首先,您在其中编写的代码runOnUiThread可以很容易地重构为一个方法,假设createDynamicUI您的代码如下所示:

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

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

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

  1. 使用 XML 定义静态 UI(到目前为止,从您的代码看来是这样!)
  2. 分配AsyncTask数据获取工作,并在完成后更新您的 UI。

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

于 2013-05-07T20:37:09.013 回答
0

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

于 2013-05-07T15:54:58.890 回答
0
 android:background="?android:attr/selectableItemBackground"

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

于 2013-05-08T10:47:41.353 回答
-1

你需要看看 AsyncTask 是如何工作

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

onPostExecute在方法中更新您的 UI

于 2013-05-07T14:59:05.573 回答