2014-02-27 39 views
-8

我希望點擊某個按鈕時指引一項活動。但是,當完成這個工作時,我的應用程序崩潰了。onclick上的錯誤

這是我的代碼,其中指出(startActivity(intent);)錯誤,

Button ExerciseButton = (Button)findViewById(R.id.button3); 

    ExerciseButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
     Intent intent = new Intent(MainActivity.this, Database.class); 
     startActivity(intent); 
     } 

    }); 

的按鈕

   <Button 
       android:id="@+id/button3" 
       android:layout_width="110dp" 
       android:layout_height="50dp" 
       android:background="@drawable/button" 
       android:onClick="grammar" 
       android:text="@string/button_exercise" 
       android:layout_marginTop="15dp" 
       android:layout_marginBottom="15dp" 
       android:layout_marginLeft="28dp" /> 

這是被稱爲活性的XML,

public class Database extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


      try { 
     String destPath = "/data/data/" + getPackageName() 
       + "/databases/questions"; 
     File f = new File(destPath); 
     if (!f.exists()) { 
      CopyDB(getBaseContext().getAssets().open("questions"), 
        new FileOutputStream(destPath)); 
     } 
    } 

      catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Toast.makeText(Database.this, "Error File", Toast.LENGTH_SHORT) 
       .show(); 
    } 
      catch (IOException e1) { 
     e1.printStackTrace(); 
     Toast.makeText(Database.this, "Error IO", Toast.LENGTH_SHORT) 
       .show(); 
    } 


     DatabaseHandler db = new DatabaseHandler(this); 


     long id = db.addQuestion("Question1", "answer1", "answer2", "answer3", "answer4", "answer"); 
     //id = db.addQuestion("Question2", "answer2"); 
     db.close(); 



     Cursor cursor = db.getAllQuestions(); 
     if (cursor.moveToFirst()) 
     { 
      do { 
       DisplayRecord(cursor); 

      } 
      while(cursor.moveToNext()); 
     } 

     db.close(); 
    } 

     public void CopyDB(InputStream inputstream, OutputStream outputstream)   
       throws IOException { 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = inputstream.read(buffer))>0){ 
       outputstream.write(buffer, 0, length); 
      } 
      inputstream.close(); 
      outputstream.close(); 
     } 

     public void DisplayRecord(Cursor cursor){ 
      Toast.makeText(this, 
        "id:" + cursor.getString(0) + "\n" + 
        "Question:" + cursor.getString(1) + "\n" + 
        "Answer1:" + cursor.getString(2) + "\n" + 
"Answer2:" + cursor.getString(3) + "\n" + 
"Answer3:" + cursor.getString(4) + "\n" + 
"Answer4:" + cursor.getString(5) + "\n" + 
"Answer:" + cursor.getString(6), 
        Toast.LENGTH_SHORT).show(); 
     } 

    } 

logcat在下面。

02-27 10:35:41.061: W/KeyCharacterMap(11469): No keyboard for id 0 
02-27 10:35:41.061: W/KeyCharacterMap(11469): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
02-27 10:35:42.471: D/AndroidRuntime(11469): Shutting down VM 
02-27 10:35:42.481: W/dalvikvm(11469): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
02-27 10:35:42.491: E/AndroidRuntime(11469): FATAL EXCEPTION: main 
02-27 10:35:42.491: E/AndroidRuntime(11469): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.grammer/com.example.grammer.Database}; have you declared this activity in your AndroidManifest.xml? 
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.app.Activity.startActivityForResult(Activity.java:2827) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.app.Activity.startActivity(Activity.java:2933) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at com.example.grammer.MainActivity$2.onClick(MainActivity.java:43) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.view.View.performClick(View.java:2485) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.view.View$PerformClick.run(View.java:9080) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.os.Handler.handleCallback(Handler.java:587) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.os.Looper.loop(Looper.java:123) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at java.lang.reflect.Method.invokeNative(Native Method) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at java.lang.reflect.Method.invoke(Method.java:507) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-27 10:35:42.491: E/AndroidRuntime(11469): at dalvik.system.NativeStart.main(Native Method) 
02-27 10:35:44.961: I/Process(11469): Sending signal. PID: 11469 SIG: 9 
+1

後logcat ........... –

+0

你把你的數據庫活動清單?請發佈您的logcat。 – Amrut

+1

在日誌中:'數據庫:你有沒有在你的AndroidManifest.xml中聲明數據庫活動?' –

回答

3

添加到您的清單文件...

<activity android:name=".Database" /> 
-2

你已經在XML設定android:onClick=grammar這樣你就不會需要實現的onclick監聽器。只需創建一個方法名稱作爲「語法」,並把你的意圖,該方法

0

你已經設置屬性onclick =「語法」在Button小部件,但在你的MainActivity沒有語法方法。可能你應該用這條路。你沒有通過像

Button b1 = (Button)findViewById(R.id.YourButton); 
b1.setonClickListener(new View.onClickListener(){ 
@Override 
    public void onClick(View view) { 
    Intent intent = new Intent(MainActivity.this, Database.class); 
    startActivity(intent); 
    } 

}); 

和BTW onclicklistener設置爲它

public void grammmer(View view) 
{ 
    // To do your stuff 
} 

就是這個樣子,註冊您的下一個活動在AndroidManifest.xml中

<activity name=".YourActivity" /> 
0

你沒有將活動添加到清單中 - 這意味着您的應用無法識別您的活動。

必須在清單文件 AndroidManifest.xml中使用<activity>元素聲明所有活動。

請閱讀更多關於它,重要的是要知道:
http://developer.android.com/training/basics/firstapp/starting-activity.html
http://developer.android.com/guide/topics/manifest/activity-element.html

如:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="your.package.name"> 
<uses-sdk android:minSdkVersion="18" /> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".Activity1" android:label="@string/app_name"></activity> 
     <activity android:name=".Activity2"></activity> 
    </application> 
    </manifest> 
0

我想問題可能是出以下一些問題...

1)在你的Database.java你已經設置setContentView(R.layout.activity_main);和你的類名是數據庫,所以確保你的XML名稱是正確的。因爲我認爲你也有main_activity.java,所以請確保兩個活動都有兩個不同的XML。並且您正在將正確的XML分配給正確的Java文件。

2)我想你還沒有在manifest文件中設置你的數據庫名稱,所以@Anil Bhatiya告訴,這樣做。