此时我需要你们的帮助,我有以下代码,但是当我在日食中运行时,它完全是错误的,(错误是在下面的代码中签名)
package com.example.search;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import com.example.database.search.DBDataSource;
import com.example.database.search.Sma;
public class EditData extends Activity implements OnClickListener
{
int hour, minute, mYear,mMonth, mDay;
static final int DATE_DIALOG_ID = 1;
private EditText txtDate;
private String[] arrMonth{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
private DBDataSource dataSource;
private long id;
private String Nama;
private String Alamat;
private Long Latitude;
private Long Longtitude;
private String Tanggal;
private String Email;
private String Website;
private String Facebook;
private String Twtitter;
private EditText edNama_sma, edAlamat_sma, edLatitude, edLongtitude, edTanggal, edEmail, edWebsite, edFacebook, edTwitter;
private TextView txId;
private Button btnSave;
private Button btnCancel;
private Sma sma;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_data);
//inisialisasi variabel
edNama_sma = (EditText) findViewById(R.id.editText_nama);
edAlamat_sma = (EditText) findViewById(R.id.editText_alamat);
edLatitude = (EditText) findViewById(R.id.editText_latitude);
edLongtitude = (EditText) findViewById(R.id.editText_longtitude);
edTanggal = (EditText) findViewById(R.id.editText_Tanggal);
edEmail = (EditText) findViewById(R.id.editText_Email);
edWebsite = (EditText) findViewById(R.id.editText_Website);
edFacebook = (EditText) findViewById(R.id.editText_Facebook);
edTwitter = (EditText) findViewById(R.id.editText_Twitter);
txId = (TextView) findViewById(R.id.text_id_barang);
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
//WHEN I ADD THIS CODE BELOW, then >> ERROR is happend >> java.lang.NullPointerExceptions
//THIS FUNCTION IS USE FOR SHOWING DATETIMEPICKER
txtDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
return true;
}
});
//UNTIL THIS LINE
dataSource = new DBDataSource(this);
dataSource.open();
// ambil data sma dari extras
Bundle bun = this.getIntent().getExtras();
id = bun.getLong("id");
Nama = bun.getString("Nama");
Alamat = bun.getString("Alamat");
Latitude = bun.getLong("Latitude");
Longtitude = bun.getLong("Longtitude");
Tanggal = bun.getString("Tanggal");
Email = bun.getString("Email");
Website = bun.getString("Website");
Facebook = bun.getString("Facebook");
Twtitter = bun.getString("Twitter");
txId.append(String.valueOf(id));
edNama_sma.setText(Nama);
edAlamat_sma.setText(Alamat);
edLatitude.setText(Latitude.toString());
edLongtitude.setText(Longtitude.toString());
edTanggal.setText(Tanggal);
edEmail.setText(Email);
edWebsite.setText(Website);
edFacebook.setText(Facebook);
edTwitter.setText(Twtitter);
//set listener pada tombol
btnSave = (Button) findViewById(R.id.button_save_update);
btnSave.setOnClickListener(this);
btnCancel = (Button) findViewById(R.id.button_cancel_update);
btnCancel.setOnClickListener(this);
}
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
{
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
String sdate = arrMonth[mMonth] + " " + LPad(mDay + "", "0", 2) + ", " + mYear;
txtDate.setText(sdate);
}
};
private static String LPad(String schar, String spad, int len)
{
String sret = schar;
for (int i = sret.length(); i < len; i++)
{
sret = spad + sret;
}
return new String(sret);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch(v.getId())
{
// apabila tombol save diklik (update barang)
case R.id.button_save_update :
String ltd = edLatitude.getText().toString();
String lotd = edLongtitude.getText().toString();
Long latitude = Long.parseLong(ltd);
Long longtitude = Long.parseLong(lotd);
sma = new Sma();
sma.setNama(edNama_sma.getText().toString());
sma.setAlamat(edAlamat_sma.getText().toString());
sma.setLatitude(latitude);
sma.setLongitude(longtitude);
sma.setEmail(edEmail.getText().toString());
sma.setTgl_berdiri(edTanggal.getText().toString());
sma.setWebsite(edWebsite.getText().toString());
sma.setFacebook(edFacebook.getText().toString());
sma.setTwitter(edTwitter.getText().toString());
sma.setId(id);
dataSource.updateBarang(sma);
Intent i = new Intent(this, ViewData.class);
startActivity(i);
EditData.this.finish();
dataSource.close();
break;
case R.id.button_cancel_update :
finish();
dataSource.close();
break;
}
}
}
任何人都可以帮助我解决这个错误,我真的要感谢所有能够解决此代码的人,谢谢:D
答案 0 :(得分:5)
您尚未初始化txtDate
EtidText
。
txtDate = (EditText) findViewById(R.id.txtDate);// or what ever you add the id for txtDate
那你就没事了。