gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:51:19 29 4
gpt4 key购买 nike

我昨天开始使用 Android studio。我对 Java 也有一些经验,主要是想通过 google 搜索我想要编写的程序。我在 stackoverflow 上被绊倒了很多次,这里的所有人都真正帮助了我:)所以首先感谢你们所有人;)

现在我的实际问题是:我想编写一个应用程序,基本上可以转换来自此网站的数据: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 param=(FrameLayout.LayoutParams)lL.getLayoutParams();

如果将 FrameLayout.LayoutParams 更改为 LinearLayout.LayoutParams,它应该可以工作。

关于java - 如何使用 JavaScript 在 Android Studio 中创建合适的 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29200222/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com