-2

我正在尝试保存输入到我的数据库中的数据,但是,我收到以下错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                                                   at ie.example.artur.adminapp.MainActivity$Send.<init>(MainActivity.java:62)

我查看了其他类似的问题并比较了我的问题并没有发现相同的问题。对不起,如果这是一个太简单的问题,但我不经常使用 java。

This is my MainActivity :



package ie.example.artur.adminapp;

import android.os.AsyncTask;
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.widget.EditText;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {


    EditText editText;
    TextView textView;
    private static final String DB_URL = "jdbc:mysql://192.168.1.6/tutorial";
    private static final String USER = "zzz";
    private static final String PASS = "zzz";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        textView = (TextView) findViewById(R.id.textView);
        editText = (EditText) findViewById(R.id.editText);

        setContentView(R.layout.activity_register);
        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 btnConn(View view) {
        Send objSend = new Send();
        objSend.execute("");


    }

    private class Send extends AsyncTask<String, String, String>

    {
        String msg = "";
        String text = editText.getText().toString();


        @Override
        protected void onPreExecute() {
            textView.setText("Please Wait Inserting Data");
        }

        @Override
        protected String doInBackground(String... strings) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
                if (conn == null) {
                    msg = "Connection goes wrong";
                } else {
                    String query = "Insert INTO student (name) VALUES('" + text + "')";
                    Statement stmt = conn.createStatement();
                    stmt.executeUpdate(query);
                    msg = "Inserting Successful!!";
                }

                conn.close();

        }

        catch(
        Exception e
        )

        {
            msg = "Connection goes Wrong";
            e.printStackTrace();

        }

        return msg;


    }



@Override
    protected void onPostExecute(String msg) {textView.setText(msg);}



    }




}

这是我的activity_register.xml:

    <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" />

</android.support.design.widget.CoordinatorLayout>
4

1 回答 1

0

将您的 setContentView 移动到活动的开头

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register

textView = (TextView) findViewById(R.id.textView); 
editText = (EditText) findViewById(R.id.editText); 
于 2017-07-16T14:15:49.903 回答