2016-04-20 107 views
-1

Im在學校爲一個項目創建一個簡單的數據庫,但是代碼本身沒有顯示錯誤,但是當我在模擬器中運行該項目時,它吐出了一個錯誤。 我認爲這個問題是從AlertDialog而產生,但林不知道什麼是錯的語法 這裏是我的代碼的副本..編譯時出現的Android MySQLite問題

package com.example.bash1.sqlitediss; 

import android.app.AlertDialog; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.view.View.OnClickListener; 

public class MainActivity extends AppCompatActivity { 

DatabaseHelper myDb; 
private Button button3; 
Button buttonviewall; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    myDb = new DatabaseHelper(this); 
    buttonviewall = (Button) findViewById(R.id.button3); 
    viewAll(); 

} 
{ 

    Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent1 = new Intent(MainActivity.this, DatabaseActivity.class); 
      startActivity(intent1); 
     } 
    }); 
    { 
    } 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
} 

public void viewAll() { 
    buttonviewall.setOnClickListener(
      new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Cursor res = myDb.getAllData(); 
        if (res.getCount() == 0) 
        // show message 
        { 
         showMessage("Error", "Nothing found"); 
         return; 
        } 

        StringBuffer buffer = new StringBuffer(); 
        while (res.moveToNext()) { 
         buffer.append("ID :" + res.getString(0) + "\n"); 
         buffer.append("Name :" + res.getString(1) + "\n"); 
         buffer.append("Datereceived :" + res.getString(2) + "\n"); 
         buffer.append("Expirydate :" + res.getString(3) + "\n\n"); 
        } 
        // Show all data 
        showMessage("Data", buffer.toString()); 

       } 
      } 

    ); 
} 


public void showMessage(String title, String Message) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setCancelable(true); 
     builder.setTitle(title); 
     builder.setMessage(Message); 
     builder.show(); 

    builder.show(); 
} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}

Activitymain.xml

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

<include layout="@layout/content_main" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email" /> 

錯誤消息日誌:

04-20 06:40:57.114 17890-17890/com.example.bash1.sqlitediss E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.example.bash1.sqlitediss, PID: 17890 
                      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bash1.sqlitediss/com.example.bash1.sqlitediss.MainActivity}: java.lang.NullPointerException 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                       at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:136) 
                       at android.app.ActivityThread.main(ActivityThread.java:5001) 
                       at java.lang.reflect.Method.invokeNative(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:515) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                       at dalvik.system.NativeStart.main(Native Method) 
                      Caused by: java.lang.NullPointerException 
                       at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68) 
                       at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:145) 
                       at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28) 
                       at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41) 
                       at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:188) 
                       at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:170) 
                       at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:502) 
                       at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:174) 
                       at com.example.bash1.sqlitediss.MainActivity.<init>(MainActivity.java:35) 
                       at java.lang.Class.newInstanceImpl(Native Method) 
                       at java.lang.Class.newInstance(Class.java:1208) 
                       at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)  
                       at android.app.ActivityThread.access$800(ActivityThread.java:135)  
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  
                       at android.os.Handler.dispatchMessage(Handler.java:102)  
                       at android.os.Looper.loop(Looper.java:136)  
                       at android.app.ActivityThread.main(ActivityThread.java:5001)  
                       at java.lang.reflect.Method.invokeNative(Native Method)  
                       at java.lang.reflect.Method.invoke(Method.java:515)  
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)  
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)  
                       at dalvik.system.NativeStart.main(Native Method)  
+0

添加AndroidManifest.xml中的所有活動 –

+0

你能說出'DatabaseActivity'的擴展嗎? –

+0

DatabaseActivity是一個獨立的類 –

回答

1

您的堆棧跟蹤中包含您發佈的RuntimeException包裝下的「由...引發」異常,並附有特定詳細信息。

無論如何,您的代碼可能導致NPE的某種方式是buttonviewall在您調用ViewAll()之前未初始化,它會調用buttonviewall上的方法。


編輯的問題後:

卸下

} 
{ 

第一}結束onCreate()方法和第二{開始是在實例初始化階段中執行一個新的初始化塊。您需要執行該代碼作爲onCreate()的一部分,因爲<init>對於例如findViewById()

+0

我看到,我發佈在主要問題的原因是它太大對於註釋部分,Ive將buttonAll()的行移動到了buttonviewall的下方,bu仍然有相同的錯誤! –

+0

應該是?我複製並粘貼問題中的整個錯誤信息。感謝您的幫助 –

+0

它鏈接到content_main.xml(我被告知基本上是相同的東西只是它的佈局) –

1

你必須在線路NullPointerException異常:2184,2233,135,1196,102你能評論這個回答下這些行。所以我可以看看他們,看看發生了什麼。

+0

林不是100%當然如何訪問這些行,找到他們 –

+0

當您運行程序和錯誤出現時,單擊紅色數字2184在在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) – SimranTea

+0

他們不可點擊,一些行是可點擊的,但這些數字不是 –