不工作尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence)'在null对象引用上

时间:2017-04-25 06:17:49

标签: java android textview

我搜查了我的案子,但是我无法在这里找到一个合适的答案新手,但仍然没有工作,所以请帮助我......

这是我的代码......

TextView TotalAmount;
int total_amount = 0;
RecyclerView dayrecyclerView;
List<Day_Details> DL;
Day_Adapter dayAdapter;
public static List<Day_Details> daysize;

 TotalAmount = (TextView) findViewById(R.id.event_total);
    TotalAmount.setText(String.valueOf(total_amount));
  public void Day(List<Day_Details> dyl) {
    //List<Day_Details>dayDetailsList

    Log.i("day response length", "dayresponse length " + dyl.size());

    if (dyl.size() > 0) {
        daysize = dyl;
        daylayout.setVisibility(LinearLayout.VISIBLE);
        DL = new ArrayList<>();
        dayAdapter = new Day_Adapter(this, DL);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        dayrecyclerView.setLayoutManager(layoutManager);
        dayrecyclerView.setItemAnimator(new DefaultItemAnimator());
        dayrecyclerView.setAdapter(dayAdapter);
        dayrecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        for (int i = 0; i < dyl.size(); i++) {
            String price = "₹ " + dyl.get(i).getDayPrice();
            Day_Details DD = new Day_Details(dyl.get(i).getId(), 
            dyl.get(i).getDayTitle(), dyl.get(i).getDayDate(), price, 
            dyl.get(i).getDayContent());
            Log.i(" Event_view ", "Day: Details "+DD.toString());
            DL.add(DD);
        }

        dayAdapter.notifyDataSetChanged();

    }


}
 public  void daychecker(Boolean ischecked,String id){

if(ischecked == true){
    for (int i = 0; i <daysize.size(); i++) {
        if(id == daysize.get(i).getId()){
           String price = daysize.get(i).getDayPrice();
           int amount = Integer.parseInt(price);
            total_amount = total_amount + amount;

        }
    }
    TotalAmount.setText("1234");
}else{
    for (int i = 0; i <=daysize.size(); i++) {
        if(id == String.valueOf(i)){
            String price = daysize.get(i).getDayPrice();
            int amount = Integer.parseInt(price);
            total_amount = total_amount - amount;
            TotalAmount.setText(String.valueOf(total_amount));
        }
    }
}
}

日子在recyclerview 当我选中复选框时,它会传递一个ID和价格 我必须在 textview (TotalAmount)中显示价格 所以当我点击复选框时,它显示错误为

04-25 11:27:29.366 9462-9462/com.appeonix.dithesh.doceventUI E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.appeonix.dithesh.doceventUI, PID: 9462
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                               at com.appeonix.doceventUI.Activity.UI.Event_View$override.daychecker(Event_View.java:258)
                                                                               at com.appeonix.doceventUI.Activity.UI.Event_View$override.access$dispatch(Event_View.java)
                                                                               at com.appeonix.doceventUI.Activity.UI.Event_View.daychecker(Event_View.java:0)
                                                                               at com.appeonix.doceventUI.Activity.Holder.Day_Holder$1.onClick(Day_Holder.java:39)
                                                                               at android.view.View.performClick(View.java:5184)
                                                                               at android.widget.CompoundButton.performClick(CompoundButton.java:128)
                                                                               at android.view.View$PerformClick.run(View.java:20910)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:145)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5942)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

这是我的Oncreateview

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event_view_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    sharedPreferences = getSharedPreferences("DocEventPreference", Context.MODE_PRIVATE);
    memid = sharedPreferences.getString("DocId", null);
    scrollView = (NestedScrollView) findViewById(R.id.Event_view_Scroll);
    //team
    teamrecyclerView = (RecyclerView) findViewById(R.id.team_recycler_view);
    //gallery
    galrecyclerView = (RecyclerView) findViewById(R.id.galleryrecycler_view);
    //day
    dayrecyclerView = (RecyclerView) findViewById(R.id.dayrecycler_view);

    //details
    About = (TextView) findViewById(R.id.abouttheevent);
    //qrcode
    imageView = (ImageView) findViewById(R.id.qrCode);
    //register
    TotalAmount = (TextView) findViewById(R.id.event_total);
    TotalAmount.setText(String.valueOf(total_amount));
    registerlay = (LinearLayout) findViewById(R.id.registerlayout);
    button = (Button) findViewById(R.id.Event_reg_confirm_btn);
    scrollView.post(new Runnable() {
        public void run() {
            scrollView.fullScroll(scrollView.FOCUS_UP);
        }
    });
    events_details = new Events_Details();
    events_details.setId(event_id);
    eventitle = (TextView) findViewById(R.id.Event_Name);
    Event_IMG = (ImageView) findViewById(R.id.backdrop);
    //LinearLayout
     eventgallerylayout = (LinearLayout) 
    findViewById(R.id.EventView_Gallery);
    eventdetailslayout = (LinearLayout) 
    findViewById(R.id.EventView_Details);
    daylayout = (LinearLayout) findViewById(R.id.EventDay_Details);
    eventteamlayout = (LinearLayout) findViewById(R.id.EventView_Team);
    eventQRlayout = (LinearLayout) findViewById(R.id.EventView_Qrcode);
    //        eventregisterlayout = (LinearLayout) 
    findViewById(R.id.EventView_Register);
    eventdetailslayout.setVisibility(LinearLayout.GONE);
    button.setVisibility(View.GONE);
    eventteamlayout.setVisibility(LinearLayout.GONE);
    daylayout.setVisibility(LinearLayout.GONE);
    eventgallerylayout.setVisibility(LinearLayout.GONE);
    //        eventQRlayout.setVisibility(LinearLayout.GONE);
   //        eventregisterlayout.setVisibility(LinearLayout.GONE);
    SingleEventJavaWebservice webservice = new SingleEventJavaWebservice(
            Event_View.this, events_details, Event_View.this);
    webservice.execute();

    initCollapsingToolbar();
}

提前致谢...

4 个答案:

答案 0 :(得分:0)

@Dithesh请您使用调试来检查您在该方法中初始化要分配的TextView值。另请检查String.valueOf(total_amount)返回正确的字符串值。

答案 1 :(得分:0)

textview的初始化应该在onCreate()

  

TotalAmount =(TextView)findViewById(R.id.event_total);

导致错误的原因。

根据您更新的问题进行修改

初始化看起来很好,所以你应该检查布局文件&#39; event_view_main.xml&#39;如果它包含带有id&#39; event_total&#39;

的TextView

答案 2 :(得分:0)

如果你没有初始化TextView,那么它会抛出一个空指针执行 您应该在创建活动时初始化TextView。

答案 3 :(得分:0)

请检查你的xml布局id&#34; event_total&#34;是否合适:

<tr />