我在Android上做我的第一个SQLite程序。该程序允许用户输入学生的详细信息并将其存储在数据库中。在调用view_data()
时会发生异常。
以下是活动的图片:
MainActivity.java:
package com.example.roopanrajesh.stud_db;
import android.app.AlertDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText et1,et2,et3;
String str1,str2,str3;
SQLiteDatabase db;
StringBuffer sb;
AlertDialog.Builder ad;
Cursor resultSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db=openOrCreateDatabase("student_db", MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(s_id VARCHAR,s_name VARCHAR,s_batch VARCHAR);");
setContentView(R.layout.activity_main);
}
public void insert_data(View view){
et1=(EditText)findViewById(R.id.editText);
et2=(EditText)findViewById(R.id.editText1);
et3=(EditText)findViewById(R.id.editText2);
str1=et1.getText().toString();
str2=et2.getText().toString();
str3=et3.getText().toString();
db.execSQL("INSERT INTO student VALUES('" + str1 + "','" + str2 + "','" + str3 + "');");
Toast.makeText(getApplicationContext(),"INSERTED",Toast.LENGTH_SHORT).show();
}
public void view_data(View view) {
try {
resultSet = db.rawQuery("SELECT * FROM student;", null);
Toast.makeText(getApplicationContext(),resultSet.getCount(), Toast.LENGTH_LONG).show();
ad = new AlertDialog.Builder(this);
if(resultSet.getCount()==0) {
Toast.makeText(getApplicationContext(), "no records", Toast.LENGTH_LONG).show();
}
while (resultSet.moveToNext()) {
sb.append("Student ID-->"+resultSet.getString(0)+"\n");
sb.append("Student Name-->"+resultSet.getString(1)+"\n");
sb.append("Student Batch-->"+resultSet.getString(2)+"\n\n");
}
ad.setTitle("Student List");
ad.setMessage(sb.toString());
}
catch (Exception e) {
Log.d("Exception", e.toString());
}
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<android.support.design.widget.TextInputLayout
android:id="@+id/sid"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_gravity="center_horizontal"
android:hint="Student ID" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/sname"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:layout_gravity="center_horizontal"
android:hint="Student Name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/sbatch"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_gravity="center_horizontal"
android:hint="Student Batch" />
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"
android:onClick="insert_data"
android:id="@+id/button2"
android:layout_gravity="right" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="view_data"
android:text="VIew"
android:id="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/button3"
android:layout_gravity="right" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:id="@+id/button4" />
</LinearLayout>
</RelativeLayout>
logcat的:
03-26 18:18:43.799 27324-27324/? D/dalvikvm: Late-enabling CheckJNI
03-26 18:18:43.929 27324-27324/com.example.roopanrajesh.stud_db W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
03-26 18:18:43.929 27324-27324/com.example.roopanrajesh.stud_db I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
03-26 18:18:43.929 27324-27324/com.example.roopanrajesh.stud_db W/dalvikvm: VFY: unable to resolve interface method 15338: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
03-26 18:18:43.929 27324-27324/com.example.roopanrajesh.stud_db D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-26 18:18:43.929 27324-27324/com.example.roopanrajesh.stud_db I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
03-26 18:18:43.929 27324-27324/com.example.roopanrajesh.stud_db W/dalvikvm: VFY: unable to resolve interface method 15342: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
03-26 18:18:43.929 27324-27324/com.example.roopanrajesh.stud_db D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-26 18:18:43.989 27324-27324/com.example.roopanrajesh.stud_db I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
03-26 18:18:43.989 27324-27324/com.example.roopanrajesh.stud_db W/dalvikvm: VFY: unable to resolve virtual method 418: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-26 18:18:43.989 27324-27324/com.example.roopanrajesh.stud_db D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-26 18:18:43.989 27324-27324/com.example.roopanrajesh.stud_db I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
03-26 18:18:43.989 27324-27324/com.example.roopanrajesh.stud_db W/dalvikvm: VFY: unable to resolve virtual method 440: Landroid/content/res/TypedArray;.getType (I)I
03-26 18:18:43.989 27324-27324/com.example.roopanrajesh.stud_db D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-26 18:18:44.029 27324-27324/com.example.roopanrajesh.stud_db I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
03-26 18:18:44.029 27324-27324/com.example.roopanrajesh.stud_db W/dalvikvm: VFY: unable to resolve virtual method 381: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
03-26 18:18:44.029 27324-27324/com.example.roopanrajesh.stud_db D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-26 18:18:44.029 27324-27324/com.example.roopanrajesh.stud_db I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
03-26 18:18:44.029 27324-27324/com.example.roopanrajesh.stud_db W/dalvikvm: VFY: unable to resolve virtual method 383: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
03-26 18:18:44.029 27324-27324/com.example.roopanrajesh.stud_db D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-26 18:18:44.109 27324-27324/com.example.roopanrajesh.stud_db I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.04.048.076_msm8226_LNX.LA.3.5.1_RB1__release_AU ()
03-26 18:18:44.109 27324-27324/com.example.roopanrajesh.stud_db I/Adreno-EGL: OpenGL ES Shader Compiler Version: E031.24.00.15
03-26 18:18:44.109 27324-27324/com.example.roopanrajesh.stud_db I/Adreno-EGL: Build Date: 08/25/14 Mon
03-26 18:18:44.109 27324-27324/com.example.roopanrajesh.stud_db I/Adreno-EGL: Local Branch:
03-26 18:18:44.109 27324-27324/com.example.roopanrajesh.stud_db I/Adreno-EGL: Remote Branch: quic/LNX.LA.3.5.1_RB1.2
03-26 18:18:44.109 27324-27324/com.example.roopanrajesh.stud_db I/Adreno-EGL: Local Patches: NONE
03-26 18:18:44.109 27324-27324/com.example.roopanrajesh.stud_db I/Adreno-EGL: Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.04.048.076 + NOTHING
03-26 18:18:44.159 27324-27324/com.example.roopanrajesh.stud_db D/OpenGLRenderer: Enabling debug mode 0
03-26 18:18:44.259 27324-27324/com.example.roopanrajesh.stud_db I/ActivityManager: Timeline: Activity_idle id: android.os.BinderProxy@41e101b8 time:236109512
03-26 18:18:50.159 27324-27324/com.example.roopanrajesh.stud_db I/ActivityManager: Timeline: Activity_idle id: android.os.BinderProxy@41e101b8 time:236115418
03-26 18:18:51.179 27324-27324/com.example.roopanrajesh.stud_db W/ResourceType: No package identifier when getting value for resource number 0x00000000
03-26 18:18:51.179 27324-27324/com.example.roopanrajesh.stud_db D/Exception: android.content.res.Resources$NotFoundException: String resource ID #0x0
答案 0 :(得分:1)
Toast.makeText(getApplicationContext(), resultSet.getCount(), Toast.LENGTH_LONG).show();
Toast.makeText
的第二个参数需要String
或@StringRes int
。您的结果是一个整数,因此它被视为字符串资源ID。
如果要显示数字,则需要先将其转换为字符串:
"" + resultSet.getCount()
使用textView.setText(...)
时同样适用。
Log.e("tag", e.toString())
不会打印堆栈跟踪。
Log.e("tag", "message", e)
。您需要将异常作为最后一个参数传递。
您发布的内容不是堆栈跟踪。