java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'boolean java.lang.String.equals(java.lang.Object)'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30684581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
提问by Blue Venrir
I'm getting error when runtime my project.
运行我的项目时出现错误。
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
This is full log :
这是完整的日志:
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.access0(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)
This is my ProductDetail.java
这是我的 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);
}
});
}
can anyone tell me where is the error and how to fix it please? if anyone can help i will be so thankful.
谁能告诉我错误在哪里以及如何修复它?如果有人可以提供帮助,我将不胜感激。
采纳答案by Farbod Salamat-Zadeh
Your String checkOrigin
is null
thus giving you a NullPointerException
. This is what is causing it:
因此,您的StringcheckOrigin
为null
您提供了一个NullPointerException
. 这就是导致它的原因:
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:
字符串checkOrigin
是null
因为您没有从您的Intent
. 这可能是因为您忘记从之前的Activity
.
但是,您可以检查您的 String 是否是null
,如果是,则不会执行这些操作。您可以通过以下方式做到这一点:
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.
因此,如果checkOrigin
为 null,则不会执行这些操作。
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.
但我建议检查Activity
您正在接收的信息Intent
,以确保您发送和接收的信息Intent
正确无误。
If you want to read more about NullPointerException
s, have a look at this question and its answers for more detail.
如果您想阅读有关NullPointerException
s 的更多信息,请查看此问题及其答案以获取更多详细信息。
回答by rahulrv
checkOrigin is null
checkOrigin 为空
String checkOrigin = i.getStringExtra("from_activity");
if(checkOrigin!=null && checkOrigin.equals("shoppinglist")){
btnAddtoShoppingList.setVisibility(View.GONE);
btnDeleteShoppingList.setVisibility(View.VISIBLE);
}
回答by Navadip Patel
Change <view> to <View>
, because view is not about empty view. It's for custom view defined through class attr, like below:
改变<view> to <View>
,因为视图不是空视图。它用于通过类 attr 定义的自定义视图,如下所示:
<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