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
答案 0 :(得分:0)
问题出现在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也这样做。