这是主要活动,我点击按钮
public class NewAddMarketingProg extends Activity implements OnClickListener {
private StartAppAd startAppAd;
String note_detail;
ListView lvNote;
String[] data;
private int mIndex;
private MarketingProgramsAdapter adapter;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* StartAppSDK.init(this, getString(R.string.account_id),
getString(R.string.app_id), true);*/
setContentView(R.layout.newaddmarketingprog);
context = this;
init();
lvNote.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
mIndex = position;
arg1.showContextMenu();
}
});
registerForContextMenu(lvNote);
startAppAd = new StartAppAd(this);
startAppAd.showAd(); // show the ad
startAppAd.loadAd(); // load the next ad
}
private void init() {
MarketingPrograms.listOfMarketingPrograms.clear();
lvNote = (ListView) findViewById(R.id.lvNotes);
getDataFromDB();
Button bAddNote = (Button) findViewById(R.id.btnAdd);
bAddNote.setOnClickListener(this);
}
private void getDataFromDB() {
DatabaseMarketingPrograms infoNote = new DatabaseMarketingPrograms(this);
infoNote.openNote();
infoNote.getDataNote();
infoNote.getRowDataNote();
infoNote.closeNote();
adapter = new MarketingProgramsAdapter(context);
lvNote.setAdapter(adapter);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bAddNote:
Intent i = new Intent(this, AddMarketingProgram.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
break;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuInflater m = getMenuInflater();
m.inflate(R.menu.noteotions, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == R.id.view) {
int id = MarketingPrograms.listOfMarketingPrograms.get(mIndex).getId();
Intent e = new Intent(this, ViewMarketingPrograms.class);
e.putExtra("id", id);
startActivity(e);
} else if (item.getItemId() == R.id.update) {
int id = MarketingPrograms.listOfMarketingPrograms.get(mIndex).getId();
Intent e = new Intent(this, AddMarketingProgram.class);
e.putExtra("mIsUpdate", true);
e.putExtra("mIndex", mIndex);
e.putExtra("id", id);
startActivity(e);
} else if (item.getItemId() == R.id.delete) {
int id = MarketingPrograms.listOfMarketingPrograms.get(mIndex).getId();
DatabaseMarketingPrograms delete = new DatabaseMarketingPrograms(NewAddMarketingProg.this);
int flag = delete.deleteEntry(id);
if (flag > 0) {
Toast.makeText(getApplicationContext(), "Deleted",
Toast.LENGTH_SHORT).show();
MarketingPrograms.listOfMarketingPrograms.remove(mIndex);
adapter.notifyDataSetChanged();
} else {
Toast.makeText(getApplicationContext(), "Could n't be Deleted",
Toast.LENGTH_SHORT).show();
}
}
return super.onContextItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
startAppAd.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
startAppAd.onPause();
}
@Override
public void onBackPressed() {
startAppAd.onBackPressed();
Intent i=new Intent(NewAddMarketingProg.this,MainActivity.class);
startActivity(i);
finish();
super.onBackPressed();
}
}
这是我想要点击按钮
打开的活动
public class AddMarketingProgram extends Activity implements OnClickListener {
TextView /* tvDate, */tvSelectDate;
EditText etName,etBudget;
Button btnSave;
boolean mIsUpdate = false;
Context context;
private int id;
private int mIndex;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addmarketingprograms);
context = this;
etName = (EditText) findViewById(R.id.etName);
etBudget = (EditText) findViewById(R.id.etBudget);
tvSelectDate = (TextView) findViewById(R.id.tvSelectDate);
tvSelectDate.setOnClickListener(this);
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
mIsUpdate = bundle.getBoolean("mIsUpdate", false);
if (mIsUpdate) {
mIndex = bundle.getInt("mIndex");
MarketingPrograms note = MarketingPrograms.listOfMarketingPrograms.get(mIndex);
String name = note.getName();
String date = note.getDate();
int budget=note.getBudget();
etName.setText(name);
tvSelectDate.setText(date);
etBudget.setText(String.valueOf(budget));
btnSave.setText("Update");
}
}
// Get Current Date and time
Calendar c = Calendar.getInstance();
dateFormatter = new SimpleDateFormat("dd,MMM yyyy");
tvSelectDate.setText(dateFormatter.format(c.getTime()));
// Initrialize date picker
setDateTimeField();
}
@Override
public void onBackPressed() {
Intent i = new Intent(context, NewAddClassNotes.class);
startActivity(i);
finish();
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnSave:
if (mIsUpdate) {
updateNote();
} else {
saveNote();
}
break;
case R.id.tvSelectDate:
fromDatePickerDialog.show();
}
}
private void updateNote() {
DatabaseMarketingPrograms update = new DatabaseMarketingPrograms(context);
update.openNote();
String name = etName.getText().toString();
String date = tvSelectDate.getText().toString();
int Budget=Integer.parseInt(etBudget.getText().toString());
boolean flag = update.editEntry(name, Budget, date, MarketingPrograms.listOfMarketingPrograms.get(mIndex).getId());
update.closeNote();
if (flag) {
Toast.makeText(getApplicationContext(), "Update Successfully",
Toast.LENGTH_LONG).show();
onBackPressed();
} else {
Toast.makeText(getApplicationContext(), "Unable to update.",
Toast.LENGTH_LONG).show();
}
}
private void saveNote() {
try {
String name = etName.getText().toString();
int budget=Integer.parseInt(etBudget.getText().toString());
String date = tvSelectDate.getText().toString();
DatabaseMarketingPrograms entry = new DatabaseMarketingPrograms(context);
entry.openNote();
long flag = entry.createEntryNote(name, budget, date);
entry.closeNote();
if (flag > 0) {
Toast.makeText(getApplicationContext(), "Save Successfully",
Toast.LENGTH_LONG).show();
onBackPressed();
} else {
Toast.makeText(getApplicationContext(), "Unable to Save.",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Unable to Save.",
Toast.LENGTH_LONG).show();
}
}
private void setDateTimeField() {
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this,
new OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
tvSelectDate.setText(dateFormatter.format(newDate
.getTime()));
}
}, newCalendar.get(Calendar.YEAR),
newCalendar.get(Calendar.MONTH),
newCalendar.get(Calendar.DAY_OF_MONTH));
}
}
答案 0 :(得分:2)
您使用ID R.id.btnAdd 来获取按钮实例,而在onClick中,您使用ID R.id.bAddNote 来执行onClick。您必须更正此问题才能使其正常工作。
这里是init()
Button bAddNote = (Button) findViewById(R.id.btnAdd); // Button ID, this seems correct, otherwise you would have got **NPE** in next line.
在onClick
case R.id.bAddNote: //different resource ID, change this to btnAdd.
您忘记为按钮ID添加大小写,或者您错误地在不同位置添加了不同的ID。我怀疑onClick
案例ID错误。
答案 1 :(得分:0)
在您的情况下,使用不同的按钮ID。
在onClick上:
案例 R.id.bAddNote ://此处替换为 R.id.btnAdd