【发布时间】:2014-04-06 03:44:35
【问题描述】:
每当我单击“添加项目”按钮时,我的应用程序就会崩溃。有什么想法吗? 这是我们如何在 android 中实现按钮调用以将数据提交到 SQLite 数据库吗? 有没有办法对值进行硬编码以测试插入是否有效?
public class DatabaseActivity extends ActionBarActivity {
EditText userName, tagNum, fabricColor, fabricType, itemComment;
SmartiWashDbAdapter smartiwashHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database);
userName=(EditText) findViewById(R.id.userNameValue);
tagNum=(EditText) findViewById(R.id.tagNumValue);
fabricColor=(EditText) findViewById(R.id.fabricColorValue);
fabricType=(EditText) findViewById(R.id.fabricTypeValue);
itemComment=(EditText) findViewById(R.id.commentValue);
smartiwashHelper= new SmartiWashDbAdapter(this);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Message.message(this, "Successfullly Iniatialized");
}
public void addItem(View view){
String addUser=userName.getText().toString();
String addTag=tagNum.getText().toString();
String addColor=fabricColor.getText().toString();
String addType=fabricType.getText().toString();
String addComment=itemComment.getText().toString();
long id=smartiwashHelper.insertData(addUser, addTag, addColor, addType, addComment);
if(id<0){
Message.message(this, "Unsuccessful");
}
else {
Message.message(this, "Successfullly Inserted");
}
}
}
这是我的 fragment.xml。我在 XML 文件中调用了我的按钮 addItem。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/userNameValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/userName" />
<EditText android:id="@+id/tagNumValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/userNameValue"
android:hint="@string/tagNumber" />
<EditText android:id="@+id/fabricColorValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tagNumValue"
android:hint="@string/fabricColor" />
<EditText android:id="@+id/fabricTypeValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fabricColorValue"
android:hint="@string/fabricType" />
<EditText android:id="@+id/commentValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fabricTypeValue"
android:hint="@string/comment" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/commentValue"
android:text="@string/addItem"
android:onClick="addItem" />
这是 logcat 文件。我收到 NullExceptionPointer 错误。这是什么意思?
04-06 03:29:25.525: E/AndroidRuntime(16166): FATAL EXCEPTION: main
04-06 03:29:25.525: E/AndroidRuntime(16166): java.lang.IllegalStateException: Could not execute method of the activity
04-06 03:29:25.525: E/AndroidRuntime(16166): at android.view.View$1.onClick(View.java:3599)
04-06 03:29:25.525: E/AndroidRuntime(16166): at android.view.View.performClick(View.java:4204)
04-06 03:29:25.525: E/AndroidRuntime(16166): at android.view.View$PerformClick.run(View.java:17354)
04-06 03:29:25.525: E/AndroidRuntime(16166): at android.os.Handler.handleCallback(Handler.java:725)
04-06 03:29:25.525: E/AndroidRuntime(16166): at android.os.Handler.dispatchMessage(Handler.java:92)
04-06 03:29:25.525: E/AndroidRuntime(16166): at android.os.Looper.loop(Looper.java:137)
04-06 03:29:25.525: E/AndroidRuntime(16166): at android.app.ActivityThread.main(ActivityThread.java:5233)
04-06 03:29:25.525: E/AndroidRuntime(16166): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 03:29:25.525: E/AndroidRuntime(16166): at java.lang.reflect.Method.invoke(Method.java:511)
04-06 03:29:25.525: E/AndroidRuntime(16166): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-06 03:29:25.525: E/AndroidRuntime(16166): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-06 03:29:25.525: E/AndroidRuntime(16166): at dalvik.system.NativeStart.main(Native Method)
04-06 03:29:25.525: E/AndroidRuntime(16166): Caused by: java.lang.reflect.InvocationTargetException
04-06 03:29:25.525: E/AndroidRuntime(16166): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 03:29:25.525: E/AndroidRuntime(16166): at java.lang.reflect.Method.invoke(Method.java:511)
04-06 03:29:25.525: E/AndroidRuntime(16166): at android.view.View$1.onClick(View.java:3594)
04-06 03:29:25.525: E/AndroidRuntime(16166): ... 11 more
04-06 03:29:25.525: E/AndroidRuntime(16166): Caused by: java.lang.NullPointerException
我修复了问题,在 addItem 方法中移动了 findById
public void addItem(View view){
userName=(EditText) findViewById(R.id.userNameValue);
tagNum=(EditText) findViewById(R.id.tagNumValue);
fabricColor=(EditText) findViewById(R.id.fabricColorValue);
fabricType=(EditText) findViewById(R.id.fabricTypeValue);
itemComment=(EditText) findViewById(R.id.commentValue);
String addUser=userName.getText().toString();
String addTag=tagNum.getText().toString();
String addColor=fabricColor.getText().toString();
String addType=fabricType.getText().toString();
String addComment=itemComment.getText().toString();
long id=smartiwashHelper.insertData(addUser, addTag, addColor, addType, addComment);
if(id<0){
Message.message(this, "Unsuccessful");
}
else {
Message.message(this, "Successfullly Inserted");
}
}
【问题讨论】:
-
这是你的第 32 行
-
这是如何解决问题的?
标签: android eclipse sqlite android-button