maybe its really stupid question. but i stuck 5 hours because this setter getter.
i have class Home_Fragment, there is value id_product in there.. this my Home_Fragment class
public class Home_Fragment extends ListFragment {
private Detail_Produk dp;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
dp = new Detail_Produk("1");
//** After this i call another activity but not Detail_Produk
}
and in my Detail_Produk class
String test;
View v;
public Detail_Produk() {
super();
}
public Detail_Produk(String test) {
super();
this.test = test;
}
public String getIDProduk() {
return test;
}
public void setIDProduk(String test) {
this.test = test;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.detail_produk, container, false);
Toast.makeText(getActivity(), "ID : " + getIDProduk().toString(), Toast.LENGTH_SHORT).show();
return v;
}
when i call activity Detail_Produk from another activity (not Home_Fragment). on line Toast its giving me error Java.Lang.NullPointerException.
my code error or i can't because i call Detail_Product Activity not from Home_Fragment?
or should i use Shared Preferences?
Thanks before :)
The problem here is that the Detail_Produk
instance that you're creating explicitly in your Home_Fragment
(and passing the test
value to) is lost as soon as the onActivityCreated()
completes.
The instance on which the onCreateView()
is actually getting called is instantiated by the Android framework when your other activity requests to start the Detail_Produk
activity. That instance does not have its test
value set and hence you get the NPE.
You should pass this value through an Intent
extra. Take a look at this Android developer tutorial on how an Activity
can retrieve data from an Intent
that started it.
http://developer.android.com/training/basics/firstapp/starting-activity.html#ReceiveIntent
dp = new Detail_Produk(1);
1
is not a string. And Detail_Produk does not have a constructor that accepts an integer argument. Not sure what's going on here. Your code should not compile, let alone barf out a NPE, which I assume is here getIDProduk().toString()
我认为您应该使用共享首选项,因此该值将保持不变。您可以在Home_Fragment中创建静态变量,然后在其中保存值并在Detail_Produk类中访问它
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.