2017-04-30 24 views
0

我试图创建Android上的简单日历,我有问题,当我尝试从MainActivity发送参数(日期,月份,年份)到另一个参数的参数是0发送日期参数到另一个活动,但值为零// android studio

MainActivity.java

public void onClick(View view) { calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView calendarView, int i, int i1, int i2) { Bundle localBundle = new Bundle(); localBundle.putInt("Date", i2); localBundle.putInt("Month", i1); localBundle.putInt("Year", i); } }); if(view == buttonGetDate1) { Bundle localBundle = new Bundle(); Intent localIntent = new Intent(MainActivity.this, TestActivity.class); localIntent.putExtras(localBundle); startActivity(localIntent); finish(); } }

TestAcvitity.java

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test); 

    datedisplay1 = (TextView) findViewById(R.id.date_display1); 
    datedisplay1.setText("Date: "); 

    buttonBack1 = (Button) findViewById(R.id.buttonBack); 
    buttonBack1.setOnClickListener(this); 
    Bundle intent = getIntent().getExtras(); 
    int day = intent.getInt("date"); 
    int month = intent.getInt("month"); 
    int year = intent.getInt("year"); 
    Log.d(TAG, "THIRD LOG : " + day + "/" + month + "/" + year); 
    datedisplay1.setText("Date: " + day + "/" + month + "/" + year); 
} 

But it's show 0/0/0

我该如何修复它? ps。对不起我的英语不好。

编辑:之后我尝试调试日志 EDIT2:更新测试活动

in this pic, intent have value are Date = 30 Year = 2017 Month = 4 but it's show null on application

回答

0

,因为你再重新初始化上点击包的数据转为空,我做你的代码中的一些变化只是给一个尝试

{ 
//inside your activity on create scope 
int dateData=0; 
int monthData=0; 
int yearData=0; 
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { 
     @Override 
     public void onSelectedDayChange(CalendarView calendarView, int i, int i1, int i2) { 
      dateData= i2; 
      monthData= i1; 
      yearData= i; 


//Log here whether u getting date here? 
     } 
    }); 


public void onClick(View view) { 

    if(view == buttonGetDate1) { 

     Bundle localBundle = new Bundle(); 
     localBundle.putInt("Date", dateData); 
     localBundle.putInt("Month", monthData); 
     localBundle.putInt("Year", yearData); 


     Intent localIntent = new Intent(MainActivity.this, TestActivity.class); 
     localIntent.putExtras(localBundle); 
     startActivity(localIntent); 
     finish(); 

    } 
    } 
}//Activity scope ends 

TestAcvitity.java

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_test); 

datedisplay1 = (TextView) findViewById(R.id.date_display1); 
datedisplay1.setText("Date: "); 

buttonBack1 = (Button) findViewById(R.id.buttonBack); 
buttonBack1.setOnClickListener(this); 
Bundle intent = getIntent().getExtras(); 
int day = intent.getInt("Date"); 
int month = intent.getInt("Month"); 
int year = intent.getInt("Year"); 
Log.d(TAG, "THIRD LOG : " + day + "/" + month + "/" + year); 
datedisplay1.setText("Date: " + day + "/" + month + "/" + year); 
} 

希望这会帮助你。

+0

感谢您的建议,但在Android监视器显示Log.d(“LOG”,“你的BUNDLE IS NULL”);我也不能按下按钮。 – MyNaMeArT

+0

好,你的包是空的吗?你是否也登录了日期选择。 – Ramz

+0

我已经更新检查 – Ramz

相关问题