【问题标题】:Storing data into SQLite database crashes when click Add Item button单击添加项目按钮时将数据存储到 SQLite 数据库崩溃
【发布时间】: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


【解决方案1】:

您必须获取按钮的 id。这里您没有获取按钮的任何 id。并相应地使用 onclicklistener。并在 onclicklistener 覆盖函数中调用 additem 函数。 希望对您有所帮助!

【讨论】:

  • 你能举个例子吗?或将我链接到教程?
【解决方案2】:

在您的 xml 文件中为按钮创建 id。

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/commentValue"
    android:text="@string/addItem"
    android:onClick="addItem" />

在你的java文件中

Button but = (Button)findViewById(R.id.button);
    but.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        additem(v);

        }
    });

【讨论】:

  • 我现在收到此错误“View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数 (new OnClickListener(){})”
  • 尝试按CTRL+SHIFT+O,IMPORT。然后在需要的地方插入分号
  • 我应该把它准确地粘贴到哪里?在 onCreate 里面?
  • 我一打开应用程序就崩溃了。
  • 我在上面发布了新的 logcat。请看一下。
【解决方案3】:
 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");
        }
}

将其复制到 oncreate 函数之外。 希望这会有所帮助!

【讨论】:

  • 我想你忘了在清单文件中添加活动。而在 java 文件中你需要扩展活动。
  • 对不起,我不明白。那我该怎么办?扩展活动是什么意思?
【解决方案4】:

我修复了问题,在 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");
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多