如何使用JavaScript在Android Studio中创建正确的UI
How to create a proper UI in Android Studio using JavaScript?
我昨天开始使用安卓工作室。我对Java也有一点经验,主要想通过谷歌搜索我想写的程序。我多次被斯塔克夫弗洛绊倒,这里的所有人都真的帮助了我:)首先感谢你们所有人;)
现在来谈谈我的实际问题:我想写一个应用程序,基本上转换数据从这个网站:http://vplanapp.ema-bonn.de/online/
我已经设法提取了所有需要的信息和我需要在后台工作的其他一切。问题是,我现在被UI卡住了,它会不断变化,因为网站正在动态更新。
无论如何,安卓工作室不断通知我多个错误,主要是因为布局。我试着在大约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.Layout Params.
这很可能在这条线上FrameLayout.LayoutParams param=(FrameLayout.LLayoutParams)lL.getLayoutParams();
如果您将FrameLayout.LayoutParams更改为LinearLayout.Layout Params.
相关文章:
- 使用导航属性创建Kendo UI网格模型的问题
- 如何在剑道UI中创建类转换效果
- 单击按钮创建多实例jQuery UI对话框
- 任何人都知道如何在asp.net telerik asp.net AJAX UI中创建自下而上的组合框
- 如何使用JavaScript在Android Studio中创建正确的UI
- 在我的选项卡jquery ui创建后回调
- 动态创建的 jQuery UI 对话框错误后 jQuery 2.2.0 升级
- 关闭动态创建的 jQuery-ui 对话框
- 如何使用 angularjs 使用 ngrepeat 和 bootstrap ui 创建分页
- 创建 UI 加载程序时的引导程序
- 使用AngularJS ui路由器创建类似弹出窗口的状态
- 如何在不创建数组的情况下使用NgFor来生成矩阵UI模式
- 在Javascript中创建自定义的可重用控件(Kendo UI)
- 如何在Jquery中使用kendo ui创建内联网格编辑
- 根据用户从jQuery UI可选下拉框中选择的内容创建链接
- jquery-ui-sortable在创建时修改html
- jquery ui自动完成combox重新创建选项不起作用
- 使用ui路由器在URL路由中创建一个必需的参数
- 在自动完成中为动态加载的内容创建UI
- 如何在Meteor 1.4中使用模板创建UI