我正在尝试构建一个可从MySQL数据库(由树莓派托管)访问表的android应用程序。并不断收到此错误...我知道我没有初始化活动,也许没有正确设置我的OnCreate ...仍然不确定我遇到的具体错误以及如何解决此错误。堆栈跟踪以及我编写的一些代码:
I/InstantRun: starting instant run server: is main process
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.notes.app_transcribe, PID: 8764
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.notes.app_transcribe/com.notes.app_transcribe.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
I/InstantRun: starting instant run server: is main process
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.notes.app_transcribe, PID: 8764
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.notes.app_transcribe/com.notes.app_transcribe.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2819)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at com.notes.app_transcribe.MainActivity.<init>(MainActivity.java:23)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
package com.notes.app_transcribe;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
private static final String url = "jdbc:mysql://10.57.158.209/NoteDB";
private static final String user = "newuser";
private static final String pass = "password";
Button btnFetch = findViewById(R.id.btnFetch),btnClear = findViewById(R.id.btnClear);
TextView txtData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtData = this.findViewById(R.id.txtData);
btnFetch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ConnectMySql connectMySql = new ConnectMySql();
connectMySql.execute("");
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtData.setText("");
}
});
}
private class ConnectMySql extends AsyncTask<String, Void, String> {
String res = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this, "Please wait...", Toast.LENGTH_SHORT)
.show();
}
@Override
protected String doInBackground(String... params) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
System.out.println("Databaseection success");
String result = "Database Connection Successful\n";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Notes detected from NoteDB");
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
result += rs.getString(1) + "\n";
}
res = result;
} catch (Exception e) {
e.printStackTrace();
res = e.toString();
}
return res;
}
@Override
protected void onPostExecute(String result) {
txtData.setText(result);
}
}
这是清单,尽管我相当确定这里没有天生的错误:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.notes.app_transcribe">
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
出于某种原因,我已经研究了此问题,并尝试了多种方法来解决该问题,但无济于事。仍然无法找到包含答案的“重复”问题,该问题解释了我如何能够纠正错误。