【问题标题】:calculation by adding textview string values通过添加 textview 字符串值进行计算
【发布时间】:2014-07-07 14:11:34
【问题描述】:
              for(int x = 1; x < i ; x++){

            int id =1000;
            int id1 = 2000;

            LinearLayout overall = new LinearLayout(this);
            overall.setOrientation(LinearLayout.VERTICAL);
            //LinearLayout
            LinearLayout llingredient = new LinearLayout(this);
            llingredient.setOrientation(LinearLayout.HORIZONTAL);
            llingredient.setLayoutParams(para4);

            LinearLayout llcal = new LinearLayout(this);
            llcal.setOrientation(LinearLayout.VERTICAL);

            LinearLayout llamt = new LinearLayout(this);
            llamt.setOrientation(LinearLayout.VERTICAL);

            //

            // ingreident name
             TextView ingredient = new TextView(this);
            ingredient.setId(x);
            ingredient.setGravity(Gravity.CENTER);
             ingredient.setLayoutParams(textcenter);



             // amount of ingredient
             TextView  amount = new TextView(this);
             TextView amt = new TextView(this);
             amount.setId(id);


             // cal for each ingredients
             TextView cal = new TextView(this);
             TextView calories = new TextView(this);

            calories.setId(id1);




             switch(x){

             case 1:

                 //ingreidents properties

                ingredient.setText("Eggs");
                ingredient.setTextSize(25);

                 //amt properties
                 amt.setText("Amt");
                 amount.setText(" 2 ");

                 // amt propertieses
                 amount.setTextSize(20);
                 amt.setTextSize(20);

                    cal.setText("Cal");
                    calories.setText("600"); 
                     // cal properties
                    cal.setTextSize(20);
                    calories.setTextSize(20);

                break;
             case 2:     
                 //ingreidents properties

                ingredient.setText("Grilled Beef Steak");
                ingredient.setTextSize(25);

                 //amt properties
                 amt.setText("Amt(g)");
                 amount.setText("100");

                 // amt propertieses
                 amount.setTextSize(20);
                 amt.setTextSize(20);

                    cal.setText("Cal");
                    calories.setText("450"); 
                     // cal properties
                    cal.setTextSize(20);
                    calories.setTextSize(20);


                break;
             case 3:
                 //ingreidents properties

                ingredient.setText("Fried Bacon ");
                ingredient.setTextSize(25);

                 //amt properties
                 amt.setText("Amt(g)");
                 amount.setText("100");

                 // amt propertieses
                 amount.setTextSize(20);
                 amt.setTextSize(20);

                    cal.setText("Cal");
                    calories.setText("350"); 
                     // cal properties
                    cal.setTextSize(20);
                    calories.setTextSize(20);



                 break;
             case 4:    
                 //ingreidents properties

                ingredient.setText("Oil");
                ingredient.setTextSize(25);

                 //amt properties
                 amt.setText("Amt(g)");
                 amount.setText("3");

                 // amt propertieses
                 amount.setTextSize(20);
                 amt.setTextSize(20);

                    cal.setText("Cal");
                    calories.setText("250"); 
                     // cal properties
                    cal.setTextSize(20);
                    calories.setTextSize(20);


                 break;
             case 5:
                 //ingreidents properties

                ingredient.setText(" lettuce ");
                ingredient.setTextSize(25);

                 //amt properties
                 amt.setText("Amt(g)");
                 amount.setText("100");

                 // amt propertieses
                 amount.setTextSize(20);
                 amt.setTextSize(20);

                    cal.setText("Cal");
                    calories.setText("90"); 
                     // cal properties
                    cal.setTextSize(20);
                    calories.setTextSize(20);


                 break;
             default: 
                 //ingreidents properties

                ingredient.setText("error");
                ingredient.setTextSize(25);

                 //amt properties
                 amt.setText("");
                 amount.setText("");

                 // amt propertieses
                 amount.setTextSize(20);
                 amt.setTextSize(20);

                    cal.setText("");

                    calories.setText("0"); 
                     // cal properties
                    cal.setTextSize(20);
                    calories.setTextSize(20);


                 break;
             }

            TextView xc =(TextView) findViewById(id1);
             int b =  Integer.parseInt(xc.getText().toString()) ;
             sum += b;

                llamt.addView(amt);
                llamt.addView(amount);
                llamt.setLayoutParams(right);

                llcal.addView(cal);
                llcal.addView(calories);
                llcal.setLayoutParams(left);

                llingredient.addView(llamt);    
                llingredient.addView(llcal);

                overall.addView(ingredient);                 
                overall.addView(llingredient);



             wrapper.addView(overall);


     id++;
     id1++;     


        }

此代码在 on create 方法中运行。基本上是一个 for 循环,它总结了卡路里的所有值 每次我尝试进入此页面时,该应用程序都会崩溃。我告诉我一个空指针异常。让我给你看日志猫:

07-07 22:06:58.872: E/AndroidRuntime(12150): FATAL EXCEPTION: main

07-07 22:06:58.872: E/AndroidRuntime(12150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.caloriecal/com.example.caloriecal.IngreCal}: java.lang.NullPointerException

07-07 22:06:58.872: E/AndroidRuntime(12150):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2183)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at android.app.ActivityThread.access$600(ActivityThread.java:144)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at android.os.Handler.dispatchMessage(Handler.java:99)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at android.os.Looper.loop(Looper.java:137)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at android.app.ActivityThread.main(ActivityThread.java:5086)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at java.lang.reflect.Method.invokeNative(Native Method)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at java.lang.reflect.Method.invoke(Method.java:511)


07-07 22:06:58.872: E/AndroidRuntime(12150):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-07 22:06:58.872: E/AndroidRuntime(12150):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)


07-07 22:06:58.872: E/AndroidRuntime(12150):    at dalvik.system.NativeStart.main(Native Method)
07-07 22:06:58.872: E/AndroidRuntime(12150): Caused by: java.lang.NullPointerException

07-07 22:06:58.872: E/AndroidRuntime(12150):    at com.example.caloriecal.IngreCal.onCreate(IngreCal.java:260)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at android.app.Activity.performCreate(Activity.java:5104)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

07-07 22:06:58.872: E/AndroidRuntime(12150):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2147)

07-07 22:06:58.872: E/AndroidRuntime(12150):    ... 11 more

【问题讨论】:

  • 发布您用于启动活动的代码。
  • 发布你得到nullpointerexception plz的行
  • 对不起,我不熟悉日志猫。但我似乎在 textview xc 添加总和代码后它崩溃了
  • 你能告诉我们 IngreCal 类的第 260 行有什么吗?
  • TextView 成分 = new TextView(this);这需要初始化。像成分 = (TextView) findViewById(R.id.ingredient);

标签: android android-layout android-activity textview


【解决方案1】:

问题出在您的 TextView 中:

TextView xc =(TextView) findViewById(id1);

您使用的 id1 应采用 R.id.theIdYouHavePlacedInYourXML 格式 我看到你正在使用

int id1 = 2000;

但您永远不应使用在 R.id 之外定义的任何值作为 findViewById() 的参数。

所以我猜你有一个带有 TextView 的布局文件,并且你在你的主要活动中扩展了这个布局(我假设是 IngreCal)。 要解决此问题,您只需为 TextView(在 xml 文件中)分配一个 id,然后在您的 java 代码中使用该 id。所以如果你有

    <TextView
        android:id="@+id/yourIdIsHere"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="5"/>

那么这将是您正确的选择:

TextView xc =(TextView) findViewById(R.id.yourIdIsHere);

您也应该对其他 TextView 执行相同操作。

【讨论】:

  • 哦,是的,我明白你的意思,但我实际上是在尝试以编程方式创建 textview 并为它们分配 int id 以便总结它们我厌倦了寻找 textview 的 id 并总结其文本
猜你喜欢
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 2011-05-11
  • 1970-01-01
相关资源
最近更新 更多