此时我想问一下,当我尝试从数据库中读取数据时出现错误,在验证活动中,错误是在下方签名。
Validation.java
public class Validation extends Activity{
private static DBDataSource dataSource;
// Error Messages
private static final String REQUIRED_MSG = "required";
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
dataSource = new DBDataSource(this);
dataSource.open();
}
//this method is to check is my textbox empty or not, the i want to modified to check is my textbox empty or equals data from database
public static boolean hasText(EditText editText) {
ArrayList<Sma> list = dataSource.getAllSma(); >> when i add this code, the apps currently stop, and the got error message java.lang.NullPointer Exception
String text = editText.getText().toString().trim();
editText.setError(null);
// length 0 means there is no text
if (text.length() == 0) {
editText.setError(REQUIRED_MSG);
return false;
}
return true;
}
这是我的DBDataSource
public ArrayList<Sma> getAllSma()
{
ArrayList<Sma> daftarSma = new ArrayList<Sma>();
// select all SQL query
Cursor cursor = database.query(DBHelper.TABLE_NAME, allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
Sma sekolah = cursorToSma(cursor);
daftarSma.add(sekolah);
cursor.moveToNext();
}
cursor.close();
return daftarSma;
}
任何人都可以帮我解决这个错误,因为我真的不知道为什么会这样。谢谢:D
这时我称之为验证活动
public class CreateData extends Activity implements OnClickListener{
private Validation valid;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.create_data);
edNama_sma = (EditText) findViewById(R.id.nama_sma);
registerViews();
}
private void registerViews() {
edNama_sma.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
valid.hasText(edNama_sma);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
@Override
public void onClick(View v)
{}
我的错误
04-05 13:54:00.727: E/AndroidRuntime(27565): FATAL EXCEPTION: main
04-05 13:54:00.727: E/AndroidRuntime(27565): java.lang.NullPointerException
04-05 13:54:00.727: E/AndroidRuntime(27565): at com.example.validation.search.Validation.hasText(Validation.java:76)
04-05 13:54:00.727: E/AndroidRuntime(27565): at com.example.search.CreateData$5.afterTextChanged(CreateData.java:230)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.widget.TextView.sendAfterTextChanged(TextView.java:7259)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8915)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:672)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:435)
04-05 13:54:00.727: E/AndroidRuntime(27565): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:333)
04-05 13:54:00.727: E/AndroidRuntime(27565): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.os.Looper.loop(Looper.java:137)
04-05 13:54:00.727: E/AndroidRuntime(27565): at android.app.ActivityThread.main(ActivityThread.java:5099)
04-05 13:54:00.727: E/AndroidRuntime(27565): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 13:54:00.727: E/AndroidRuntime(27565): at java.lang.reflect.Method.invoke(Method.java:511)
04-05 13:54:00.727: E/AndroidRuntime(27565): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
04-05 13:54:00.727: E/AndroidRuntime(27565): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
04-05 13:54:00.727: E/AndroidRuntime(27565): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
活动Validation
尚未初始化且尚未调用onCreate()
。因此,datasource
为null
,您获得NPE。
根据您发布的内容,Validation
根本不应该是一项活动。您应该只为实际活动的类扩展Activity
,在清单中列出并通过Intent
实例化。
对于需要有效Context
的方法(例如SQLiteOpenHelper
构造函数),将其作为参数传递,而不是尝试使用未实例化的this
的{{1}}。< / p>