编译时Android MySQLite问题

时间:2016-04-20 09:46:00

标签: android sqlite

我正在为学校的项目创建一个简单的数据库,但是代码本身没有显示错误,但是当我在模拟器中运行项目时,它会发出错误。 我认为问题源于AlertDialog,但我不确定语法是否有问题 这是我的代码的副本..

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) 

2 个答案:

答案 0 :(得分:1)

你在第2184,2233,135,1196,102行有一个NullPointerException。你可以在这个答案下评论这些行。所以我可以看看它们,看看发生了什么。

答案 1 :(得分:1)

你的stacktrace有&#34;由&#34;在您发布的RuntimeException包装器下面的例外情况。

无论如何,您的代码可能导致NPE的某种方式是buttonviewall在您调用ViewAll()调用buttonviewall上的方法之前未初始化} {

编辑后的问题:

删除

}

第一个onCreate()结束{方法,第二个onCreate()启动在实例初始化阶段执行的新初始化程序块。您需要在<init>中执行该代码,因为findViewById()对于例如java.lang.NoClassDefFoundError: org/wso2/carbon/identity/application/authentication/framework/internal/FrameworkServiceComponent at com.mycompany.wso2.identity.application.authentication.framework.handler.provisioning.impl.CustomProvisioningHandler.handle(CustomProvisioningHandler.java:75) at org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl.DefaultStepBasedSequenceHandler.handleJitProvisioning(DefaultStepBasedSequenceHandler.java:714) at org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl.DefaultStepBasedSequenceHandler.handlePostAuthentication(DefaultStepBasedSequenceHandler.java:424) at org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl.DefaultStepBasedSequenceHandler.handle(DefaultStepBasedSequenceHandler.java:156) at org.wso2.carbon.identity.application.authentication.framework.handler.request.impl.DefaultAuthenticationRequestHandler.handle(DefaultAuthenticationRequestHandler.java:115) at org.wso2.carbon.identity.application.authentication.framework.handler.request.impl.DefaultRequestCoordinator.handle(DefaultRequestCoordinator.java:135) at org.wso2.carbon.identity.application.authentication.framework.CommonAuthenticationHandler.doPost(CommonAuthenticationHandler.java:46) at org.wso2.carbon.identity.application.authentication.framework.CommonAuthenticationHandler.doGet(CommonAuthenticationHandler.java:37) at org.wso2.carbon.identity.sso.saml.servlet.SAMLSSOProviderServlet.sendRequestToFramework(SAMLSSOProviderServlet.java:1013) at org.wso2.carbon.identity.sso.saml.servlet.SAMLSSOProviderServlet.sendToFrameworkForAuthentication(SAMLSSOProviderServlet.java:457) at org.wso2.carbon.identity.sso.saml.servlet.SAMLSSOProviderServlet.handleSPInitSSO(SAMLSSOProviderServlet.java:360) at org.wso2.carbon.identity.sso.saml.servlet.SAMLSSOProviderServlet.handleRequest(SAMLSSOProviderServlet.java:195) at org.wso2.carbon.identity.sso.saml.servlet.SAMLSSOProviderServlet.doPost(SAMLSSOProviderServlet.java:107) at javax.servlet.http.HttpServlet.service(HttpServlet.java:646) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.eclipse.equinox.http.helper.ContextPathServletAdaptor.service(ContextPathServletAdaptor.java:37) at org.eclipse.equinox.http.servlet.internal.ServletRegistration.service(ServletRegistration.java:61) at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(ProxyServlet.java:128) at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:60) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.wso2.carbon.tomcat.ext.servlet.DelegationServlet.service(DelegationServlet.java:68) ... at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.ClassNotFoundException: org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceComponent cannot be found by custom_wso2_1.0_1.0.0 at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501) at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421) at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412) at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 58 more 来说太早了{{1}}。