简体   繁体   中英

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

I started yesterday with Android studio. I have a little experience with Java as well and mainly wanted to google my way through the programm I want to write. I stumbled multiple times over stackoverflow and all of the people here really helped me out :) so thanks to all of you first of all ;)

Now to my actual problem: I want to write an app that basically converts data from this website: http://vplanapp.ema-bonn.de/online/

I already managed to extract all the needed information and everything else I need working in the background. The thing is, that I am now stuck with the UI, which will be constantly changing, because the website is being dynamically updated.

Anyway, Android Studio keeps informing me of multiple errors, mainly because of the layouts. I was trying to fix them all, over about 4 hours, but somehow it all just got worse and more complicated :(

I hope some of you guys would take the time to analyse (my really screwed up) source code :) thanks in advance!

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);
    }
}

And here the error log:

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)

I know my code is really messed up, but maybe you guys could give me some hints on how to improve :)

What the error is telling you is that you are trying to cast an object which is of type FrameLayout.LayoutParams to LinearLayout.LayoutParams.

This is most likely on this line FrameLayout.LayoutParams param=(FrameLayout.LayoutParams)lL.getLayoutParams();

And it should work if you change FrameLayout.LayoutParams to LinearLayout.LayoutParams.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM