gpt4 book ai didi

java - 无法确定 NullPointerException 的原因

转载 作者:行者123 更新时间:2023-11-29 21:10:43 26 4
gpt4 key购买 nike

我有下面这个购物车类,它被编码为显示购物车的内容。产品以线性布局显示,在 textViews 中包含产品信息和相应的按钮以删除产品,并在线性布局中水平排列。 View 元素是动态创建的。在调用此类之前,我的应用程序运行良好。然后应用程序崩溃,LogCat 在 "lb.addView(ll)" 行给出了一个 Java.lang.NullPointerException 错误。我不知道为什么会这样。有人请帮我解决这个问题。

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();
}
}

最佳答案

很可能你的 lb 是空的,因为调用

final LinearLayout lb          = (LinearLayout) findViewById(R.id.linearMain);

没有像你预期的那样通过 id 找到它。

关于java - 无法确定 NullPointerException 的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22962432/

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