お友達はどうですか..単純なAndroidアプリをデータベースに接続しようとしているので、緊張しすぎています。しかし、まったく機能していません。このサイトから多くの役立つ資料を見つけました。あなたも私を助けてくれることを願っています。
here are the codes for each file. I can also see my db file in data/data directiry.
my SQLquery file ....
package com.example.hotornot;
//import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
//import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
//import android.widget.TextView;
public class SQLquery {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_HOTNESS = "persons_hotness";
public static final String DATABASE_NAME = "HotOrNotdb";
public static final String DATABASE_TABLE = "peopleTle";
private static final int DATABASE_VERSION = 3;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper
{
public DbHelper(Context context) {
super(context, DATABASE_NAME,null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
private static final String DATABASE_CREATE = "CREATE TABLE proper ("+
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT NOT NULL,"+
"hotness TEXT NOT NULL"+
"); ";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT NOT NULL, " + KEY_HOTNESS + " TEXT NOT NULL); ");
db.execSQL(DATABASE_CREATE);
}
private static final int old = 1;
private static final int nwver = 3;
public void onUpgrade(SQLiteDatabase db, int old, int nwvwer) {
// TODO Auto-generated method stub
db.execSQL(" DROP TABLE IF EXISTS proper");
//onCreate(db);
}
}
public SQLquery (Context c)
{
ourContext = c;
}
public SQLquery open() throws SQLException
{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close()
{
ourHelper.close();
}
public long createEntry(String nam, String hotnes) {
ContentValues cv = new ContentValues();`enter code here`
cv.put("name",nam);
cv.put("hotness", hotnes);
return ourDatabase.insert("proper", null, cv);
}
}
==========================================================================
My "hotornot" file...
package com.example.hotornot;
import android.app.Activity;
import android.app.Dialog;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class hotorNot extends Activity implements OnClickListener
{
Button sqlUpdate, sqlView;
EditText sqlName, sqlHotness;
Boolean didItWork;
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotor_not);
sqlUpdate = (Button) findViewById(R.id.button1);
sqlView = (Button) findViewById(R.id.button2);
sqlName = (EditText) findViewById(R.id.editText1);
sqlHotness = (EditText) findViewById(R.id.editText2);
sqlView.setOnClickListener(this);
sqlUpdate.setOnClickListener(this);
}catch(Exception exp)
{
didItWork = false;
Dialog d = new Dialog(this);
d.setTitle("sdfasf");
TextView tv = new TextView(this);
tv.setText("sdfsdf"+exp.toString());
d.setContentView(tv);
d.show();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hotor_not, menu);
return true;
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
try{
String nam = sqlName.getText().toString();
String Hotnes = sqlHotness.getText().toString();
SQLquery sqll = new SQLquery(hotorNot.this);
sqll.open();
sqll.createEntry(nam,Hotnes);
sqll.close();
Dialog d = new Dialog(this);
d.setTitle("In Button");
TextView tv = new TextView(this);
tv.setText("Button Pressed");
d.setContentView(tv);
d.show();
}catch(SQLiteException e)
{
didItWork = false;
Dialog d = new Dialog(this);
d.setTitle("Failed"+e.toString());
TextView tv = new TextView(this);
tv.setText("Failed"+e.toString());
d.setContentView(tv);
d.show();
}finally{
if(didItWork){
Dialog d = new Dialog(this);
d.setTitle("Success");
TextView tv = new TextView(this);
tv.setText("Succeeded");
d.setContentView(tv);
d.show();
}
}
break;
}
// TODO Auto-generated method stub
}
}
===========================================================
"AndroidManifest" file
=============================================================
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hotornot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".hotorNot"
android:label="@string/title_activity_hotor_not" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
==========================================================================
First i was getting sqlite exception that my database version is 3 and i have puted wrongly there 1 ... but when i had corrected it . than on clicking to button my apps creashes giving an error of "Unfortunatly hotornot has stopped"
And here are the error logs...
07-13 19:43:09.484: E/AndroidRuntime(586): FATAL EXCEPTION: main
07-13 19:43:09.484: E/AndroidRuntime(586): java.lang.NullPointerException
07-13 19:43:09.484: E/AndroidRuntime(586): at com.example.hotornot.hotorNot.onClick(hotorNot.java:80)
07-13 19:43:09.484: E/AndroidRuntime(586): at android.view.View.performClick(View.java:3511)
07-13 19:43:09.484: E/AndroidRuntime(586): at android.view.View$PerformClick.run(View.java:14105)
07-13 19:43:09.484: E/AndroidRuntime(586): at android.os.Handler.handleCallback(Handler.java:605)
07-13 19:43:09.484: E/AndroidRuntime(586): at android.os.Handler.dispatchMessage(Handler.java:92)
07-13 19:43:09.484: E/AndroidRuntime(586): at android.os.Looper.loop(Looper.java:137)
07-13 19:43:09.484: E/AndroidRuntime(586): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-13 19:43:09.484: E/AndroidRuntime(586): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 19:43:09.484: E/AndroidRuntime(586): at java.lang.reflect.Method.invoke(Method.java:511)
07-13 19:43:09.484: E/AndroidRuntime(586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-13 19:43:09.484: E/AndroidRuntime(586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-13 19:43:09.484: E/AndroidRuntime(586): at dalvik.system.NativeStart.main(Native Method)
07-13 19:43:10.094: I/dalvikvm(586): threadid=3: reacting to signal 3
07-13 19:43:10.134: I/dalvikvm(586): Wrote stack traces to '/data/anr/traces.txt'
Please help me...
Thanks ....