java.lang.NullPointerException。试图在一个空对象引用上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)'。[英] java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

问题描述

我在运行时我的项目收到错误.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.olympic/com.prima.olympic.ProductDetail}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

这是完整日志:

06-06 23:12:45.561: E/AndroidRuntime(17135): FATAL EXCEPTION: main
06-06 23:12:45.561: E/AndroidRuntime(17135): Process: com.example.olympic, PID: 17135
06-06 23:12:45.561: E/AndroidRuntime(17135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.olympic/com.prima.olympic.ProductDetail}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread.access$900(ActivityThread.java:177)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.os.Looper.loop(Looper.java:145)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread.main(ActivityThread.java:5942)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at java.lang.reflect.Method.invoke(Native Method)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at java.lang.reflect.Method.invoke(Method.java:372)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
06-06 23:12:45.561: E/AndroidRuntime(17135): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
06-06 23:12:45.561: E/AndroidRuntime(17135):    at com.prima.olympic.ProductDetail.onCreate(ProductDetail.java:85)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.Activity.performCreate(Activity.java:6289)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)

这是我的productdetail.java

public class ProductDetail extends Activity {
Button btnAddtoShoppingList;
Button btnDeleteShoppingList;
TextView detail_productType;
TextView detail_wdh;
TextView detail_volume;
TextView detail_weight;
TextView detail_cont20;
TextView detail_cont40;
TextView detail_pack;
TextView detail_quantity;
TextView detail_colour;
TextView detail_cetegories;
TextView detail_series;
TextView detail_price;
ImageView detail_imageView;

String shown_id, shown_type, shown_wdh, shown_volume, shown_weight, shown_cont20, shown_cont40, shown_pack, shown_quantity, shown_colour, shown_categories, shown_series, shown_price;
String customer_id, error_message ;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_detail);

    btnAddtoShoppingList = (Button)findViewById(R.id.btnAddtoShoppingList);
    btnDeleteShoppingList = (Button)findViewById(R.id.btnDeleteShoppingList);
    SharedPreferences userInformation = getApplicationContext().getSharedPreferences("userinfo", 0);
    customer_id = userInformation.getString("customer_id", "none");

    detail_productType = (TextView)findViewById(R.id.detail_productType);
    detail_wdh = (TextView)findViewById(R.id.detail_wdh);
    detail_volume = (TextView)findViewById(R.id.detail_volume);
    detail_weight = (TextView)findViewById(R.id.detail_weight);
    detail_cont20 = (TextView)findViewById(R.id.detail_cont20);
    detail_cont40 = (TextView)findViewById(R.id.detail_cont40);
    detail_pack = (TextView)findViewById(R.id.detail_pack);
    detail_quantity = (TextView)findViewById(R.id.detail_quantity);
    detail_colour = (TextView)findViewById(R.id.detail_colour);
    detail_cetegories = (TextView)findViewById(R.id.detail_categories);
    detail_series = (TextView)findViewById(R.id.detail_series);
    detail_price = (TextView)findViewById(R.id.detail_price);
    detail_imageView = (ImageView)findViewById(R.id.detail_imageView);

    // Get Value from previous activity
    Intent i = getIntent();
    shown_id = i.getStringExtra("shown_id");
    shown_type = i.getStringExtra("shown_type");
    shown_wdh = i.getStringExtra("shown_wdh");
    shown_volume = i.getStringExtra("shown_volume");
    shown_weight = i.getStringExtra("shown_weight");
    shown_cont20 = i.getStringExtra("shown_cont20");
    shown_cont40 = i.getStringExtra("shown_cont40");
    shown_pack = i.getStringExtra("shown_pack");
    shown_quantity = i.getStringExtra("shown_quantity");
    shown_colour = i.getStringExtra("shown_colour");
    shown_categories = i.getStringExtra("shown_categories");
    shown_series = i.getStringExtra("shown_series");
    shown_price = i.getStringExtra("shown_price");

    String checkOrigin = i.getStringExtra("from_activity");
    if(checkOrigin.equals("shoppinglist")){
        btnAddtoShoppingList.setVisibility(View.GONE);
        btnDeleteShoppingList.setVisibility(View.VISIBLE);
    }

    Bitmap bitmap = (Bitmap) i.getParcelableExtra("shown_bitmap");

    detail_productType.setText("PRODUCT TYPE        : "+shown_type);
    detail_wdh.setText("W x D x H                                   : "+shown_wdh);
    detail_volume.setText("VOLUME                                   : "+shown_volume);
    detail_weight.setText("WEIGHT                                       : "+shown_weight);
    detail_cont20.setText("CONT20                                   : "+shown_cont20);
    detail_cont40.setText("CONT40                                   : "+shown_cont40);
    detail_pack.setText("PACK                                               : "+shown_pack);
    detail_quantity.setText("QUANTITY                           : "+shown_quantity);
    detail_colour.setText("COLOUR                                   : "+shown_colour);
    detail_cetegories.setText("CATEGORIES                   : "+shown_categories);
    detail_series.setText("SERIES                                           : "+shown_series);
    detail_price.setText("PRICE                                             : Rp "+shown_price);
    detail_imageView.setImageBitmap(bitmap);



    btnAddtoShoppingList.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v)
        {
            addtoShoppingListTask addtoShoppingList = new addtoShoppingListTask();
            addtoShoppingList.execute((Void) null);

        }
    });

    btnDeleteShoppingList.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v)
        {
            deleteShoppingListTask deletetoShoppingList = new deleteShoppingListTask();
            deletetoShoppingList.execute((Void) null);

        }
    });

}

谁能告诉我错误在哪里以及如何解决它?如果有人可以帮助我会非常感谢.

推荐答案

您的字符串checkOrigin是null因此给您一个NullPointerException.这是导致它的原因:

String checkOrigin = i.getStringExtra("from_activity");
if(checkOrigin.equals("shoppinglist")){
    btnAddtoShoppingList.setVisibility(View.GONE);
    btnDeleteShoppingList.setVisibility(View.VISIBLE);
}

字符串checkOrigin是null,因为您没有从Intent从中接收任何值.这可能是因为你忘了通过你以前的Activity的价值.
但是,您可以检查您的字符串是否为null,如果是,则不会执行这些操作.您将通过以下方式执行此操作:

String checkOrigin = i.getStringExtra("from_activity");
if(checkOrigin != null && checkOrigin.equals("shoppinglist")){
    btnAddtoShoppingList.setVisibility(View.GONE);
    btnDeleteShoppingList.setVisibility(View.VISIBLE);
}

因此,如果checkOrigin为null,则不会执行这些操作.

但我建议检查你正在接收Intent的Activity,以确保正确地发送和接收Intent.

如果要了解更多关于NullPointerException S,请查看此问题及其答案"

其他推荐答案

更改<view> to <View>,因为视图不是空视图.它是通过类attr定义的自定义视图:

<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.your.package.YourCustomView" />

,你有错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

其他推荐答案

checkorigin为null

String checkOrigin = i.getStringExtra("from_activity");
        if(checkOrigin!=null && checkOrigin.equals("shoppinglist")){
            btnAddtoShoppingList.setVisibility(View.GONE);
            btnDeleteShoppingList.setVisibility(View.VISIBLE);
        }

本文地址:https://www.itbaoku.cn/post/978679.html