2012-09-08 22 views
-2

我已經在一些網站上下載了一些示例程序,並試圖執行該程序。那時候我沒有找到類Exception.i試圖在很多方面解決錯誤。但我不能得到解決方案。如何解決classNotFoundException?

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(); 
    } 

    } 

logcat的:

 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 

main.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> 

的Manifest.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> 

感謝事先

+0

請把你的logcat的。 –

+0

我們需要看到你的logcat的輸出甚至開始有機會找出發生了什麼。 – Barak

+0

@ V.J。我已經上傳LOGCAT – USER5762

回答

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" 

我寫上面的解決方案,請找到它..

+0

s謝謝vj然後我無法將數據存儲在數據庫中 – USER5762

+0

上面的代碼將數據保存在數據庫中。我已經在我的系統中測試過它。 –

+0

如何搜索數據庫中的數據請在android – USER5762