按钮上的NullPointerException单击(Android)

时间:2016-02-16 13:37:00

标签: android android-activity nullpointerexception onclicklistener android-button

首先,我知道NullPointerException的含义是什么,我检查了有关该主题的类似问题,但没有任何帮助。

MainActivity.java

public class MainActivity extends AppCompatActivity
{
    private TextView enterNumberOfFloorsTextView;
    private EditText numberOfFloorsEditText;
    private Button goToFloorsActivityButton;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initializeView();

        goToFloorsActivityButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(MainActivity.this, FloorActivity.class);
                intent.putExtra("NumberOfFloors",numberOfFloorsEditText.getText().toString());
                startActivity(intent);
            }
        });
    }

    public void initializeView()
    {
        enterNumberOfFloorsTextView = (TextView) findViewById(R.id.enter_number_of_floors_text_view_id);
        numberOfFloorsEditText = (EditText) findViewById(R.id.number_of_floors_edit_text_id);
        goToFloorsActivityButton = (Button) findViewById(R.id.go_to_floors_activity_button_id);
    }
}

我初始化了按钮,我也检查了它不是null。所以它绝对不是空的。

我认为问题出在清单或我想要开始的活动中。 也许LogCat可以提供帮助。

logcat的

   02-16 15:37:48.788: E/SQLiteLog(3461): (1) near "EXIST": syntax error
02-16 15:37:48.792: E/AndroidRuntime(3461): FATAL EXCEPTION: main
02-16 15:37:48.792: E/AndroidRuntime(3461): Process: com.dl.my.lift.bag.liftmybag, PID: 3461
02-16 15:37:48.792: E/AndroidRuntime(3461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dl.my.lift.bag.liftmybag/com.dl.my.lift.bag.liftmybag.FloorActivity}: android.database.sqlite.SQLiteException: near "EXIST": syntax error (code 1): , while compiling: DROP TABLE IF EXIST floorsTable
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.app.ActivityThread.access$800(ActivityThread.java:148)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.os.Looper.loop(Looper.java:135)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.app.ActivityThread.main(ActivityThread.java:5272)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at java.lang.reflect.Method.invoke(Native Method)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at java.lang.reflect.Method.invoke(Method.java:372)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
02-16 15:37:48.792: E/AndroidRuntime(3461): Caused by: android.database.sqlite.SQLiteException: near "EXIST": syntax error (code 1): , while compiling: DROP TABLE IF EXIST floorsTable
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:894)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:505)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1775)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1706)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at com.dl.my.lift.bag.liftmybag.FloorsDatabase.onUpgrade(FloorsDatabase.java:50)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at com.dl.my.lift.bag.liftmybag.FloorsDatabase.getAllFloors(FloorsDatabase.java:120)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at com.dl.my.lift.bag.liftmybag.FloorActivity.onCreate(FloorActivity.java:51)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.app.Activity.performCreate(Activity.java:5977)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-16 15:37:48.792: E/AndroidRuntime(3461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
02-16 15:37:48.792: E/AndroidRuntime(3461):     ... 10 more

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dl.my.lift.bag.liftmybag.MainActivity">

    <TextView
        android:id="@+id/enter_number_of_floors_text_view_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/enter_number_of_floors_text_view_text"
        android:textAllCaps="false"
        />

    <EditText
        android:id="@+id/number_of_floors_edit_text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="40dp"
        android:layout_below="@id/enter_number_of_floors_text_view_id"
        />

    <Button
        android:id="@+id/go_to_floors_activity_button_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/go_to_floors_activity_button_text"
        android:textAllCaps="false"
        android:layout_below="@id/number_of_floors_edit_text_id"
        />

</RelativeLayout>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dl.my.lift.bag.liftmybag">

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        <activity android:name=".FloorActivity"></activity>
    </application>

</manifest>

问题在于,我在应用程序上有一个工作Sqlite数据库,然后我从主要活动中删除了他并将所有代码(初始化过程和向某些表添加行)放到活动中我要开始。然后我写了这个MainActivity,曾经是一个工作的数据库。 也许那是什么造成了所有麻烦?

修改 我编辑了logcat。它确实说了Sqlite

EDIT2 FloorAcivity.java

package com.dl.my.lift.bag.liftmybag;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class FloorActivity extends AppCompatActivity implements View.OnClickListener
{
    private Button floor12Button;
    private Button floor11Button;
    private Button floor10Button;
    private Button floor9Button;
    private Button floor8Button;
    private Button floor7Button;
    private Button floor6Button;
    private Button floor5Button;
    private Button floor4Button;
    private Button floor3Button;
    private Button floor2Button;
    private Button floor1Button;

    private SmsManager smsManager;

    private static final String SOMETHING_WENT_WRONG_TAG = "Something went wrong";

    List<Floor> floors;

    FloorsDatabase floorsDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_floor);

        initializeView();

        setOnClickListeners();

        smsManager = SmsManager.getDefault();

        floorsDatabase = new FloorsDatabase(FloorActivity.this);
        //floorsDatabase.addFloor(new Floor(2.2,7,"","Nothing special"));
        floors = new ArrayList<Floor>();
        floors = floorsDatabase.getAllFloors();
        for (Floor floor : floors) // For each floor in floors
        {
            floor1Button.setText(floors.toString());
        }

    }

    public void initializeView()
    {
        floor12Button = (Button) findViewById(R.id.button_1_id);
        floor11Button = (Button) findViewById(R.id.button_2_id);
        floor10Button = (Button) findViewById(R.id.button_3_id);
        floor9Button = (Button) findViewById(R.id.button_4_id);
        floor8Button = (Button) findViewById(R.id.button_5_id);
        floor7Button = (Button) findViewById(R.id.button_6_id);
        floor6Button = (Button) findViewById(R.id.button_7_id);
        floor5Button = (Button) findViewById(R.id.button_8_id);
        floor4Button = (Button) findViewById(R.id.button_9_id);
        floor3Button = (Button) findViewById(R.id.button_10_id);
        floor2Button = (Button) findViewById(R.id.button_11_id);
        floor1Button = (Button) findViewById(R.id.button_12_id);
    }

    public void setOnClickListeners()
    {
        this.floor12Button.setOnClickListener(this);
        this.floor11Button.setOnClickListener(this);
        this.floor10Button.setOnClickListener(this);
        this.floor9Button.setOnClickListener(this);
        this.floor8Button.setOnClickListener(this);
        this.floor7Button.setOnClickListener(this);
        this.floor6Button.setOnClickListener(this);
        this.floor5Button.setOnClickListener(this);
        this.floor4Button.setOnClickListener(this);
        this.floor3Button.setOnClickListener(this);
        this.floor2Button.setOnClickListener(this);
        this.floor1Button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        int buttonId = v.getId();

        switch (buttonId)
        {
            case R.id.button_1_id: // Floor 12
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_2_id: // Floor 11
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_3_id: // Floor 10
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_4_id: // Floor 9
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_5_id: // Floor 8
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_6_id: // Floor 7
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_7_id: // Floor 6
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_8_id: // Floor 5
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_9_id: // Floor 4
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_10_id: // Floor 3
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_11_id: // Floor 2
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            case R.id.button_12_id: // Floor 1
            {
                this.smsManager.sendTextMessage("+972523508838",null,"Hi Kids",null,null); // Send SMS to a phone number

                break;
            }
            default:
            {
                Log.i(SOMETHING_WENT_WRONG_TAG,"Something went wrong");
                break;
            }
        }
    }
}

0 个答案:

没有答案