2017-01-20 17 views
0

我從父級CouponsActivity中的片段foodCouponsFragment打開IndividualCouponsActivity。用意向打開IndividualCouponsActivity。打開後,我想編輯IndividualCouponActivity的textViews和ImageViews。應該指出的是,IndividualCouponActivity不是該片段的父代。如何編輯由同一片段打開的片段打開的活動中的值

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 

       Coupon coupon = foodCoupons.get(position); 

       Intent foodCouponsIntent = new Intent(getActivity(), IndividualCouponActivity.class); 
       startActivity(foodCouponsIntent); 

       IndividualCouponActivity activity = new IndividualCouponActivity(); 

       activity.setValue(coupon.getCouponValue()); 

       activity.setCompany(coupon.getCompanyName()); 

       activity.setInfo(coupon.getDescription()); 

       activity.setPts(coupon.getPts()); 

       activity.setQr(coupon.getPicImageResourceId()); 

     } 
    });  

但是,當我運行該應用程序時,單擊listView使應用程序關閉。這是該日誌當時說:

FATAL EXCEPTION: main 
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase. 

我懷疑這是從我使用新IndividualCouponActivity活動的訪問類的方法生根。謝謝!

回答

0

不要用新創建的活動......

傳遞優惠券對象到你的活動是這樣的:

Intent intent = new Intent(); 
Bundle bundle = new Bundle(); 
bundle.putParcelable("coupon", coupon); // or putSerializable() 
intent.putExtras(bundle); 
intent.setClass(context, IndividualCouponActivity.class); 
context.startActivity(intent); 

在你的活動中onCreate()你可以得到優惠券通過用:

getIntent().getExtras().getParcelable("coupon"); // or putSerializable() 

然後將值設置爲您的視圖。

0
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 

      Coupon coupon = (Coupon) adapterView.getItemAtPosition(position); 

      Intent foodCouponsIntent = new Intent(getActivity(), IndividualCouponActivity.class); 

      Bundle extras = new Bundle(); 
      extras.put("value", coupon.getCouponValue()); 
      extras.put("companyName", coupon.getCompanyName()); 
      extras.put("description",coupon.getDescription()); 
      extras.put("pts", coupon.getPts()); 
      extras.put("picImageResourceId", coupon.picImageResourceId()); 

      foodCouponsIntent.putExtras(extras); 


      startActivity(foodCouponsIntent); 

    } 
}); 

,然後在IndividualCouponActivity的onCreate:

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //get reference to views... 
     textViewValue = (TextView) findViewById(R.id.textViewValue); 

     //do this for all views.... 


     //if all objects are String values, if not you know what you have to do... 
     //here you get values from extras 

     Bundle data = getIntent().getExtras(); 
     String value = data.getStringExtra("value"); 
     String companyName = data.getStringExtra("companyName"); 
     String description = data.getStringExtra("description"); 
     String pts = data.getStringExtra("pts"); 
     String picImageResourceId = data.getStringExtra("picImageResourceId"); 


     //here update UI views 
     //example 

     textViewValue.setText(value); 

     //do this for all views.... 
} 

也許有一些錯字的錯誤,但是這是解決方案...

0

可以使用通過這兩個活動之間的數據「putExtra 「

Intent intent = new Intent(getBaseContext(), IndividualCouponActivity.class); 
intent.putExtra("COUPON_COMPANY_NAME", coupon.getCompanyName()); 
startActivity(intent);