我在android中写了这段代码我得到错误

时间:2011-06-10 09:59:52

标签: android-layout

06-10 14:57:26.289: ERROR/AndroidRuntime(608): FATAL EXCEPTION: main
06-10 14:57:26.289: ERROR/AndroidRuntime(608): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.sudhakar.sqlite.database.project/com.android.sudhakar.sqlite.database.project.MainActivity}: java.lang.NullPointerException
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at android.os.Looper.loop(Looper.java:123)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at java.lang.reflect.Method.invokeNative(Native Method)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at java.lang.reflect.Method.invoke(Method.java:521)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at dalvik.system.NativeStart.main(Native Method)
06-10 14:57:26.289: ERROR/AndroidRuntime(608): Caused by: java.lang.NullPointerException
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at com.android.sudhakar.sqlite.database.project.MainActivity.onCreate(MainActivity.java:37)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     ... 11 more
06-10 14:57:26.360: WARN/ActivityManager(69):   Force finishing activity com.android.sudhakar.sqlite.database.project/.MainActivity
06-10 14:57:27.096: WARN/ActivityManager(69): Activity pause timeout for HistoryRecord{44e80808 com.android.sudhakar.sqlite.database.project/.MainActivity}
package com.android.sudhakar.sqlite.database.project;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity
{

    Button insButton;
    Button delButton;
    Button srchButton;
    Button updateButton;


    private DBHelper dbHelper;
    private EditText firstEditText;
    private EditText lastEditText;
    private EditText branchEditText;
    private EditText rollEditText;
    private EditText sscEditText;
    private EditText hscEditText;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);

        int rollNo = Integer.parseInt(rollEditText.toString()); 
        String fName = firstEditText.toString();
        String lName = lastEditText.toString();
        String branch = branchEditText.toString();
        float ssc = Float.parseFloat(sscEditText.toString());
        float hsc = Float.parseFloat(hscEditText.toString());

        final Object[] object  = {rollNo,fName,lName,branch,ssc,hsc};

        setContentView(R.layout.page);

        try 
        {
            String sql = "insert into studsinfo";
            dbHelper = new DBHelper(this);

            dbHelper.execSql(sql, object);

            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
            alertBuilder.setTitle("Student Information..");
            final AlertDialog alert = alertBuilder.create();
            alert.show();

            insButton.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    Intent intent = new Intent(MainActivity.this, DeleteActivity.class);
                    intent.putExtra("id", object.toString());
                    startActivity(intent);
                }
            });
        }

        catch (Exception e) 
        {
            e.printStackTrace();
        }

        dbHelper.close();
    }
}

1 个答案:

答案 0 :(得分:1)

读取异常中的堆栈跟踪:

06-10 14:57:26.289: ERROR/AndroidRuntime(608): Caused by: java.lang.NullPointerException
06-10 14:57:26.289: ERROR/AndroidRuntime(608):     at com.android.sudhakar.sqlite.database.project.MainActivity.onCreate(MainActivity.java:37)

在第37行,您正在尝试访问字段或调用null对象变量上的方法。看起来你正在使用一些未初始化的字段。