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

前端 未结 3 1960
忘掉有多难
忘掉有多难 2021-01-17 06:18

I\'m getting error when runtime my project.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.olympic/com.prima.olympic.ProductD         


        
相关标签:
3条回答
  • 2021-01-17 06:30

    checkOrigin is null

    String checkOrigin = i.getStringExtra("from_activity");
            if(checkOrigin!=null && checkOrigin.equals("shoppinglist")){
                btnAddtoShoppingList.setVisibility(View.GONE);
                btnDeleteShoppingList.setVisibility(View.VISIBLE);
            }
    
    0 讨论(0)
  • 2021-01-17 06:45

    Your String checkOrigin is null thus giving you a NullPointerException. This is what is causing it:

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

    The String checkOrigin is null because you are not receiving any value from your Intent. This might be because you forgot to pass a value from your previous Activity.
    However, you can check to see if your String is null and if it is, then those actions won't be performed. You would do this by the following:

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

    Therefore, if checkOrigin is null, then those actions won't be performed.

    But I would recommend checking the Activity that you are receiving the Intent from to make sure that you are sending and receiving the Intent correctly.

    If you want to read more about NullPointerExceptions, have a look at this question and its answers for more detail.

    0 讨论(0)
  • 2021-01-17 06:46

    Change <view> to <View>, because view is not about empty view. It's for custom view defined through class attr, like below:

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

    And you got error

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    
    0 讨论(0)
提交回复
热议问题