我有这个问题从意图访问额外。
我解析的值是long
类型,我需要将此值存储在数据库中。
所以这就是我到目前为止:
MainActivity:
package com.example.calendar;
import java.sql.Date;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CalendarView;
import static android.provider.BaseColumns._ID;
import static com.example.calendar.Constants.TABLE_NAME;
import static com.example.calendar.Constants.TIME;
import static com.example.calendar.Constants.TITLE;
import static com.example.calendar.Constants.DETAILS;
import static com.example.calendar.Constants.DATE;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity implements OnClickListener {
private AppointmentsData appointments;
CalendarView calendar;
Date date = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View createButton = findViewById(R.id.btn_create);
View editButton = findViewById(R.id.btn_viewEdit);
View deleteButton = findViewById(R.id.btn_delete);
View moveButton = findViewById(R.id.btn_move);
View searchButton = findViewById(R.id.btn_search);
View translateButton = findViewById(R.id.btn_translate);
View exitButton = findViewById(R.id.exit);
createButton.setOnClickListener(this);
editButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
moveButton.setOnClickListener(this);
searchButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
exitButton.setOnClickListener(this);
appointments = new AppointmentsData(this);
calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
date = new Date(year, month, dayOfMonth);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i;
switch (v.getId()) {
case R.id.btn_create:
i = new Intent(this, CreateAppointment.class);
i.putExtra(DATE, date.getTime());
startActivity(i);
break;
case R.id.btn_viewEdit:
i = new Intent(this, EditViewAppointment.class);
i.putExtra(DATE, date.getTime());
startActivity(i);
break;
case R.id.btn_move:
i = new Intent(this, MoveAppointment.class);
i.putExtra(DATE, date.getTime());
startActivity(i);
break;
case R.id.btn_delete:
i = new Intent(this, DeleteAppointment.class);
i.putExtra(DATE, date.getTime());
startActivity(i);
break;
case R.id.btn_search:
i = new Intent(this, SearchAppointment.class);
startActivity(i);
break;
case R.id.btn_translate:
i = new Intent(this, TranslateAppointment.class);
i.putExtra(DATE, date.getTime());
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
另一个使用该值的Activity:
package com.example.calendar;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
import static android.provider.BaseColumns._ID;
import static com.example.calendar.Constants.TABLE_NAME;
import static com.example.calendar.Constants.TIME;
import static com.example.calendar.Constants.TITLE;
import static com.example.calendar.Constants.DETAILS;
import static com.example.calendar.Constants.DATE;
import static com.example.calendar.Constants.CONTENT_URI;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class CreateAppointment extends Activity implements OnClickListener{
private static String[] FROM = { _ID, DATE, TIME, TITLE, DETAILS};
private static String ORDER_BY = TIME + " ASC";
AppointmentsData appointments;
CalendarView calendar;
String string;
EditText nameTextBox;
EditText timeTextBox;
EditText detailsTextBox;
Button createButton;
SQLiteDatabase db;
Intent fetchDate = getIntent();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
createButton = (Button) findViewById(R.id.apptSave);
nameTextBox = (EditText)findViewById(R.id.apptName);//Assign the global name box
timeTextBox = (EditText)findViewById(R.id.apptTime);//Assign the global time box
detailsTextBox = (EditText)findViewById(R.id.apptDetails);//Assign the global details box
calendar = (CalendarView)findViewById(R.id.calendar);
createButton.setOnClickListener(this);
appointments = new AppointmentsData(this);
string = "row";
long dateFecth = fetchDate.getLongExtra(DATE, defaultValue);
}
private void addAppointment(String string) {
/* Insert a new record into the Events data
source. You would do something similar
for delete and update. */
String getTitle = nameTextBox.getText().toString();
String getTime = timeTextBox.getText().toString();
String getDetails = detailsTextBox.getText().toString();
db = appointments.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DATE, calendar.getDate());
values.put(TIME, getTime);
values.put(TITLE, getTitle);
values.put(DETAILS, getDetails);
getContentResolver().insert(CONTENT_URI, values);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.apptSave:
addAppointment(string);
finish();
break;
}
}
}
错误发生在long dateFecth = fetchDate.getLongExtra(DATE, defaultValue);
行,我不知道第二个参数会用什么,因为我想到的一切都给了我一个错误。
有人可以帮帮我吗?
答案 0 :(得分:5)
将fetchDate = getIntent();
内的onCreate
方法移至{/ p>
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
//.your code here..
fetchDate = getIntent(); // get intent here from previous Activity
long dateFecth = fetchDate.getLongExtra(DATE, defaultValue);
}
答案 1 :(得分:1)
首先检查 - 值与给定名称相关联。 如果您不想使用defaultValue,请尝试使用bundle,如下所示
if (getchDate.hasExtra("Date")) {
Bundle bundle = new Bundle();
bundle = getchDate.getExtras();
Object object = bunlde.get(Date);
Now parse this object in your desire type.
}
答案 2 :(得分:0)
您应该首先检查getIntent()
的结果是否为空(当然,调用应该在onCreate
内,而不是构造函数)。如果它不是空的,那么从中获取长变量。 defaultValue
表示如果找不到指定键的值,则从方法返回的值。