java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean java.lang.String.equals(java.lang.Object)'

时间:2015-06-06 15:30:36

标签: java android nullpointerexception

我在运行项目时遇到错误。

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

        }
    });

}

任何人都可以告诉我错误在哪里以及如何解决它?如果有人可以提供帮助,我会非常感激。

3 个答案:

答案 0 :(得分:11)

更改<view android:layout_width="wrap_content" android:layout_height="wrap_content" class="com.your.package.YourCustomView" /> ,因为视图与空视图无关。它是通过类attr定义的自定义视图,如下所示:

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

你得到了错误

onchange=tram_stops(this.value)

答案 1 :(得分:7)

您的字符串checkOriginnull,因此会为您提供NullPointerException。这就是造成它的原因:

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

字符串checkOriginnull,因为您未从Intent收到任何值。这可能是因为您忘记传递之前Activity的值 但是,您可以检查您的字符串是否为null,如果是,则不会执行这些操作。您可以通过以下方式执行此操作:

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

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

但我建议您查看收到Activity的{​​{1}},以确保您正确发送和接收Intent

如果您想了解有关Intent的更多信息,请查看this question and its answers for more detail

答案 2 :(得分:0)

checkOrigin为空

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