-2

我已经在某些站点下载了一些示例程序并尝试执行该程序。那时我没有找到类异常。我试图以多种方式解决该错误。但我无法得到解决方案。

    public class DataBaseDemoActivity extends Activity {
   /** Called when the activity is first created. */
SQLiteDatabase db;
Button btnInsert;

     @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnInsert=(Button)findViewById(R.id.button1);

    try{

    db=openOrCreateDatabase("StudentDB",SQLiteDatabase.CREATE_IF_NECESSARY,null);

    db.execSQL("Create Table Temp(id integer,name text)");

    }catch(SQLException e)
    {

    }

    btnInsert.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {


            EditText eid=(EditText) findViewById(R.id.edit1);
            EditText ename=(EditText)findViewById(R.id.edit2);

            ContentValues values=new ContentValues();
            values.put("id", eid.getText().toString());
            values.put("name", ename.getText().toString());
            if((db.insert("temp", null, values))!=-1)
            {
Toast.makeText(DataBaseDemoActivity.this, "Record       Successfully Inserted", 2000).show();
            }
            else
            {
Toast.makeText(DataBaseDemoActivity.this, "Insert Error", 2000).show();
            }
            eid.setText("");
            ename.setText("");

            Cursor c=db.rawQuery("SELECT * FROM temp",null);

            c.moveToFirst();
            while(!c.isAfterLast())
            {
           Toast.makeText(  DataBaseDemoActivity.this,c.getString(0)+ " "+c.getString(1), 1000).show();
                c.moveToNext();
            }
            c.close();
        }
    });
   }

   @Override
    protected void onStop() {

    db.close();
    super.onStop();
    }

    }

日志猫:

     09-08 11:15:09.841: E/AndroidRuntime(932): FATAL EXCEPTION: main
     09-08 11:15:09.841: E/AndroidRuntime(932): java.lang.RuntimeException: Unable to    instantiate activity                        
   ComponentInfo{com.example.databasedemo/com.example.databasedemo.MainActivity}:                                  java.lang.ClassNotFoundException: com.example.databasedemo.MainActivity
     09-0 8 11:15:09.841: E/AndroidRuntime(932):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
     09-08 11:15:09.841: E/AndroidRuntime(932):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
      09-08 11:15:09.841: E/AndroidRuntime(932):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
      09-08 11:15:09.841: E/AndroidRuntime(932):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
     09-08 11:15:09.841: E/AndroidRuntime(932):     at android.os.Handler.dispatchMessage(Handler.java:99)
      09-08 11:15:09.841: E/AndroidRuntime(932):    at android.os.Looper.loop(Looper.java:137)
      09-08 11:15:09.841: E/AndroidRuntime(932):    at android.app.ActivityThread.main(ActivityThread.java:4745)
     09-08 11:15:09.841: E/AndroidRuntime(932):     at java.lang.reflect.Method.invokeNative(Native Method)
     09-08 11:15:09.841: E/AndroidRuntime(932):     at java.lang.reflect.Method.invoke(Method.java:511)
       09-08 11:15:09.841: E/AndroidRuntime(932):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    09-08 11:15:09.841: E/AndroidRuntime(932):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    09-08 11:15:09.841: E/AndroidRuntime(932):  at dalvik.system.NativeStart.main(Native Method)
    09-08 11:15:09.841: E/AndroidRuntime(932): Caused by: java.lang.ClassNotFoundException: com.example.databasedemo.MainActivity
     09-08 11:15:09.841: E/AndroidRuntime(932):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
     09-08 11:15:09.841: E/AndroidRuntime(932):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
     09-08 11:15:09.841: E/AndroidRuntime(932):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
     09-08 11:15:09.841: E/AndroidRuntime(932):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
      09-08 11:15:09.841: E/AndroidRuntime(932):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
      09-08 11:15:09.841: E/AndroidRuntime(932):    ... 11 more

主.XML:

 <?xml version="1.0" encoding="UTF-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" 
  android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView 
     android:layout_width="fill_parent"
      android:layout_height="wrap_content"
       android:text="@string/hello"/>
 <TextView 
android:id="@+id/textView1" 
android:textAppearance="?android:attr/textAppearanceMedium"
 android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
 android:text="@string/si" 
 android:layout_marginTop="20dp"/>
 <EditText 
     android:id="@+id/edit1"
      android:layout_height="wrap_content"
       android:layout_width="match_parent" 
       android:inputType="number" 
       android:hint="@string/esi"
        >

        <requestFocus android:layout_width="wrap_content" />

        </EditText>
        <TextView 
            android:id="@+id/textView2" 
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:layout_height="wrap_content"
             android:layout_width="wrap_content"
              android:text="@string/sn"/>
        <EditText 
            android:id="@+id/edit2"
             android:layout_height="wrap_content"
              android:layout_width="match_parent" 
              android:hint="@string/esn"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ir" />

   </LinearLayout>

清单.XML:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.databasedemo"
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=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

提前致谢

4

1 回答 1

2
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.databasedemo"
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=".DataBaseDemoActivity"
         android:label="@string/title_activity_main" >
         <intent-filter>
             <action android:name="android.intent.action.MAIN" />

             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
     </activity>
 </application>

请更换

android:name=".MainActivity"

android:name=".DataBaseDemoActivity"

我已经写了上面的解决方案请找到它..

于 2012-09-08T06:51:22.393 回答