如何使用 JavaScript 在 Android Studio 中创建合适的 UI?

How to create a proper UI in Android Studio using JavaScript?

我昨天开始使用 Android 工作室。我对 Java 也有一点经验,主要是想 google 完成我想编写的程序。我在 Whosebug 上多次跌跌撞撞,这里的所有人都真的帮助了我 :) 所以首先要感谢你们所有人 ;)

现在解决我的实际问题: 我想写一个基本上转换来自这个网站的数据的应用程序:http://vplanapp.ema-bonn.de/online/

我已经设法提取所有需要的信息以及我需要在后台工作的所有其他信息。问题是,我现在坚持使用 UI,它将不断变化,因为网站正在动态更新。

无论如何,Android Studio 不断通知我多个错误,主要是因为布局。我试图在大约 4 小时内修复它们,但不知何故,一切都变得更糟、更复杂:(

我希望你们中的一些人能花时间分析(我真的搞砸了)源代码:)提前致谢!

package vplan.ema;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;


public class VPlan extends ActionBarActivity {
    public int Stufe=0;
    public String Klasse="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final String MyPref = "ema.VPlan.PREFERENCE_FILE_KEY";
        Context context=this;
        SharedPreferences pref = this.getSharedPreferences(MyPref, context.MODE_PRIVATE);
        Klasse=pref.getString("Klasse", "No name defined");
        Stufe=pref.getInt("Stufe", 0);
        super.onCreate(savedInstanceState);
        if(Stufe==0) {
            letsgo();
        }
        if(Stufe<=9){
            new newdoc().execute("http://vplanapp.ema-bonn.de/online/index.php?get="+Stufe+Klasse);
        }else {
            switch (Stufe){
                case 10:
                    new newdoc().execute("http://vplanapp.ema-bonn.de/online/index.php?get=EF");
                break;
                case 11:
                    new newdoc().execute("http://vplanapp.ema-bonn.de/online/index.php?get=Q1");
                break;
                case 12:
                    new newdoc().execute("http://vplanapp.ema-bonn.de/online/index.php?get=Q2");
                break;
                default:
                   letsgo();
                break;
            }
        }

        setContentView(R.layout.activity_vplan);


    }

    private class newdoc extends AsyncTask<String, Void, String[]> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        String[] end;
        @Override
        protected String[] doInBackground(String... params){
            try{
                Connection conn=Jsoup.connect(params[0]);
                Document doc = conn.get();
                String title = doc.title();
                System.out.println(title);
                final String MyPref = "ema.VPlan.PREFERENCE_FILE_KEY";
                Context context=VPlan.this;
                SharedPreferences pref = VPlan.this.getSharedPreferences(MyPref, context.MODE_PRIVATE);
                Klasse=pref.getString("Klasse", "No name defined");
                Stufe=pref.getInt("Stufe", 0);
                for(Element table:doc.select("table#table")){
                    if(table.getAllElements().size()<=1){
                        end=new String[1];
                        end[0]=("Keine Vertretungen in der ");
                        if (Stufe<=9) {
                            end[0]=end[0]+(Stufe + Klasse);
                        }else{
                            switch (Stufe){
                                case 10:
                                    end[0]=end[0]+("EF");
                                break;
                                case 11:
                                    end[0]=end[0]+("Q1");
                                break;
                                case 12:
                                    end[0]=end[0]+("Q2");
                                break;
                                default:
                                    end[0]=("Keine Klasse angegeben");
                                break;
                            }
                        }
                        return end;
                    }
                    int i=0;
                     for(Element row:table.select("tr")){
                        for (Element tds:row.select("td")){
                            i++;
                        }
                     }
                    end=new String[i];
                    i=0;
                     for(Element row:table.select("tr")){
                        for (Element tds:row.select("td")){
                            end[i]=tds.text();
                            i++;
                        }
                     }
                }

            }catch(Exception e){
                Log.d("doInBackground exception", e.toString());
            }
            return end;
        }
        @Override
        protected void onPostExecute(String[] result) {
            if(result.length>1){
            LinearLayout lL=(LinearLayout)findViewById(R.id.lLayout);

            LinearLayout lL2=new LinearLayout(VPlan.this);
            lL.addView(lL2);
            FrameLayout.LayoutParams param=(FrameLayout.LayoutParams)lL.getLayoutParams();
            ViewGroup.LayoutParams param2=findViewById(R.id.textView).getLayoutParams();

            param.height=75;
            lL2.setLayoutParams(param);
            lL2.setBackgroundColor(-16777216);
            lL2.setOrientation(LinearLayout.HORIZONTAL);

            for(int i=0;i<result.length;i++) {
                if(result[i].equals("+")) {

                    lL2=new LinearLayout(VPlan.this);
                    lL.addView(lL2);
                    param=(FrameLayout.LayoutParams)lL.getLayoutParams();
                    param.height=75;
                    lL2.setLayoutParams(param);
                    lL2.setBackgroundColor(-16777216);
                    lL2.setOrientation(LinearLayout.HORIZONTAL);

                    param2.width=50;
                }else{if(result[i].length()>9){
                    TextView tv=new TextView(VPlan.this);
                    lL2.addView(tv);
                    tv.setText(result[i]);
                    tv.setLayoutParams(param2);
                    tv.setTextAppearance(VPlan.this, android.R.style.TextAppearance_Large);
                    tv.setTextColor(16777215);
                    tv.setGravity(Gravity.CENTER);
                    tv.setClickable(false);
                    tv.setFocusable(false);

                    lL2=new LinearLayout(VPlan.this);
                    lL.addView(lL2);
                    param=(FrameLayout.LayoutParams)lL.getLayoutParams();
                    param.height=75;
                    lL2.setLayoutParams(param);
                    lL2.setBackgroundColor(-16777216);
                    lL2.setOrientation(LinearLayout.HORIZONTAL);

                    param2.width=50;
                    }else{
                        TextView tv=new TextView(VPlan.this);
                        tv.setText(result[i]);
                        lL2.addView(tv);
                        tv.setLayoutParams(param2);
                        tv.setTextAppearance(VPlan.this, android.R.style.TextAppearance_Large);
                        tv.setTextColor(16777215);
                        tv.setGravity(Gravity.CENTER_VERTICAL);
                        tv.setClickable(false);
                        tv.setFocusable(false);
                        param2.width=100;
                        }
                    }
                }
            }else{
                LinearLayout lL=(LinearLayout)findViewById(R.id.lLayout);

                LinearLayout lL2=new LinearLayout(VPlan.this);

                FrameLayout.LayoutParams param=(FrameLayout.LayoutParams)lL.getLayoutParams();
                param.height=0;
                lL2.setLayoutParams(param);
                lL2.setBackgroundColor(-16777216);
                lL2.setOrientation(LinearLayout.HORIZONTAL);lL.addView(lL2);

                TextView tv=new TextView(VPlan.this);
                tv.setText(result[0]);
                System.out.println(result[0]);
                lL.addView(tv);
                ViewGroup.LayoutParams param2=findViewById(R.id.textView).getLayoutParams();
                tv.setLayoutParams(param2);
                tv.setTextAppearance(VPlan.this, android.R.style.TextAppearance_Large);
                tv.setTextColor(16777215);
                tv.setGravity(Gravity.CENTER);
                tv.setClickable(false);
                tv.setFocusable(false);
            }

        }
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_vplan, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void letsgo(){
        Intent intent = new Intent(this, Stufen.class);
        startActivity(intent);
    }
}

这里是错误日志:

03-22 22:04:56.882  28131-28131/vplan.ema W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4161ad88)
03-22 22:04:56.882  28131-28131/vplan.ema E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: vplan.ema, PID: 28131
    java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:669)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16628)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16628)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16628)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
            at android.view.View.measure(View.java:16628)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16628)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16628)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2298)
            at android.view.View.measure(View.java:16628)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5622)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:212)
            at android.app.ActivityThread.main(ActivityThread.java:5135)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
            at dalvik.system.NativeStart.main(Native Method)

我知道我的代码真的很乱,但也许你们可以给我一些改进的提示:)

错误告诉您的是,您正在尝试将类型为 FrameLayout.LayoutParams 的对象转换为 LinearLayout.LayoutParams。

最有可能在这条线上 FrameLayout.LayoutParams 参数=(FrameLayout.LayoutParams)lL.getLayoutParams();

如果您将 FrameLayout.LayoutParams 更改为 LinearLayout.LayoutParams,它应该会起作用。