我有以下课程,这是一个购物车类,并编码显示购物车的内容。产品以线性布局显示,文本视图中包含产品信息,相应按钮用于删除产品,并在水平布局中水平排列。视图元素是动态创建的。我的应用程序运行良好,直到调用此类。然后应用程序崩溃,LogCat在Java.lang.NullPointerException
行发出"lb.addView(ll)"
错误。我不知道为什么会发生这种情况。有人请帮我解决这个问题。
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Secondscreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
//TextView showCartContent = (TextView) findViewById(R.id.showCart);
final Button thirdBtn = (Button) findViewById(R.id.third);
final LinearLayout lb = (LinearLayout) findViewById(R.id.linearMain);
final Controller aController = (Controller) getApplicationContext();
final int cartSize = aController.getCart().getCartSize();
//String showString = "";
if(cartSize > 0){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i = 0; i < cartSize; i++) {
// Get product data from product data arraylist
String pName = aController.getCart().getProducts(i).getProductName();
int pPrice = aController.getCart().getProducts(i).getProductPrice();
String pDisc = aController.getCart().getProducts(i).getProductDesc();
// Create LinearLayout to view elements
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
TextView product = new TextView(this);
product.setText(" " + pName + " ");
// Add textView to LinearLayout
ll.addView(product);
TextView productdesc = new TextView(this);
product.setText(" " + pDisc + " ");
// Add textView to LinearLayout
ll.addView(productdesc);
TextView price = new TextView(this);
price.setText(" $" + pPrice + " ");
// Add textView to LinearLayout
ll.addView(price);
final Button btn = new Button(this);
btn.setId(i + 1);
btn.setText("Remove from Cart");
// set the layoutParams on the button
btn.setLayoutParams(params);
final int index = i;
//Create click listener for dynamically created button
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Get product instance for index
ModelProducts tempProductObject = aController.getCart().getProducts(index);
aController.getCart().removeProducts(tempProductObject);
Toast.makeText(getApplicationContext(),"Products Removed \n Now Cart size: "+ aController.getCart().getCartSize(), Toast.LENGTH_LONG).show();
}
});
// Add button to LinearLayout
ll.addView(btn);
// Add LinearLayout to XML layout
lb.addView(ll);
}
}
thirdBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(cartSize > 0) {
Intent i = new Intent(getBaseContext(), Thirdscreen.class);
startActivity(i);
} else
Toast.makeText(getApplicationContext(), "Shopping cart is empty.", Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
答案 0 :(得分:2)
由于
的调用,您的lb
很可能为空
final LinearLayout lb = (LinearLayout) findViewById(R.id.linearMain);
没有像你期望的那样通过id找到它。
答案 1 :(得分:0)
请检查您在创建视图时是否创建了歧义如果您尝试使用相同的ID定义视图,则可能会导致问题