无法启动 activity:java.lang.NullPointerException
Unable to start activity: java.lang.NullPointerException
我试图从数据库中检索员工数据。我在数据库连接期间遇到这些错误。单击按钮后,出现以下错误:
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.brs, PID: 15799
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brs/com.example.myhospital.AboutUs}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:220)
at java.util.Arrays$ArrayList.<init>(Arrays.java:3738)
at java.util.Arrays.asList(Arrays.java:3725)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:165)
at com.example.myhospital.AboutUsListView.<init>(AboutUsListView.java:39)
at com.example.myhospital.AboutUs.onCreate(AboutUs.java:46)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 15799 SIG: 9
这是关于我们 activity 的 Java 代码,它在列表视图中显示图像和一些数据。
AboutUs.java
package com.example.myhospital;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class AboutUs extends AppCompatActivity {
String[] name;
String[] email;
String[] imagepath;
ListView listView;
BufferedInputStream is;
String line=null;
String result=null;
String urladdress = "http://192.168.1.66/brs/team.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_us);
listView=(ListView)findViewById(R.id.lview);
StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
collectData();
AboutUsListView aboutUsListView=new AboutUsListView(this,name,email,imagepath);
listView.setAdapter(aboutUsListView);
}
private void collectData()
{
try{
URL url=new URL(urladdress);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
is=new BufferedInputStream(con.getInputStream());
}
catch (Exception ex)
{
ex.printStackTrace();
}
try{
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuilder sb=new StringBuilder();
while ((line=br.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
}
catch (Exception ex)
{
ex.printStackTrace();
}
try{
JSONArray ja=new JSONArray(result);
JSONObject jo=null;
name=new String[ja.length()];
email=new String[ja.length()];
imagepath=new String[ja.length()];
for(int i=0;i<=ja.length();i++){
jo=ja.getJSONObject(i);
name[i]=jo.getString("name");
email[i]=jo.getString("email");
imagepath[i]=jo.getString("photo");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
AboutUsListView.java
package com.example.myhospital;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.InputStream;
public class AboutUsListView extends ArrayAdapter<String> {
private String[] name;
private String[] email;
private String[] imagepath;
private Activity context;
Bitmap bitmap;
public AboutUsListView (Activity context, String[] name, String[] email, String[] imagepath) {
super(context, R.layout.layout, name);
this.context = context;
this.name = name;
this.email = email;
this.imagepath = imagepath;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
View r=convertView;
ViewHolder viewHolder=null;
if(r==null){
LayoutInflater layoutInflater=context.getLayoutInflater();
r=layoutInflater.inflate(R.layout.layout,null,true);
viewHolder=new ViewHolder(r);
r.setTag(viewHolder);
}
else {
viewHolder=(ViewHolder)r.getTag();
}
viewHolder.tvw1.setText(name[position]);
viewHolder.tvw2.setText(email[position]);
new GetImageFromURL(viewHolder.ivw).execute(imagepath[position]);
return r;
}
class ViewHolder{
TextView tvw1;
TextView tvw2;
ImageView ivw;
ViewHolder(View v){
tvw1=(TextView)v.findViewById(R.id.dname);
tvw2=(TextView)v.findViewById(R.id.dphone);
ivw=(ImageView)v.findViewById(R.id.imageView);
}
}
public class GetImageFromURL extends AsyncTask<String,Void, Bitmap>
{
ImageView imgView;
public GetImageFromURL(ImageView imgv)
{
this.imgView=imgv;
}
@Override
protected Bitmap doInBackground(String... url) {
String urldisplay = url[0];
bitmap = null;
try{
InputStream ist=new java.net.URL(urldisplay).openStream();
bitmap= BitmapFactory.decodeStream(ist);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap){
super.onPostExecute(bitmap);
imgView.setImageBitmap(bitmap);
}
}
}
这是关于我们页面的 XML 代码
AboutUs.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DFD8D8"
tools:context=".AboutUs">
<TextView
android:id="@+id/textView"
android:layout_width="418dp"
android:layout_height="62dp"
android:background="@color/red"
android:text="Available Ambulance Details"
android:textAlignment="center"
android:textColor="@android:color/background_light"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<ListView
android:id="@+id/lview"
android:layout_width="368dp"
android:layout_height="495dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.488"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.477" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的 XML 布局代码。
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#ffffff"
android:padding="15dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="86dp"
android:layout_weight="1" />
<TextView
android:id="@+id/dname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginTop="16dp"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:textColor="#333"
android:textSize="22dp"
/>
<TextView
android:id="@+id/dphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginTop="50dp"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:textColor="#333"
android:textSize="22dp"
/>
</RelativeLayout>
</LinearLayout>
您正在用空值初始化 AboutUsListView
Class。 ArrayAdapter
的构造不允许空值,如 documentation:
中所述
objects T: The objects to represent in the ListView. This value cannot be null.
您应该检查名称数组是否为空并将其替换为空数组:
public AboutUsListView (Activity context, String[] name, String[] email, String[] imagepath) {
super(context, R.layout.layout, name==null?new String[0]:name);
this.context = context;
this.name = name;
this.email = email;
this.imagepath = imagepath;
}
我试图从数据库中检索员工数据。我在数据库连接期间遇到这些错误。单击按钮后,出现以下错误:
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.brs, PID: 15799
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brs/com.example.myhospital.AboutUs}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:220)
at java.util.Arrays$ArrayList.<init>(Arrays.java:3738)
at java.util.Arrays.asList(Arrays.java:3725)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:165)
at com.example.myhospital.AboutUsListView.<init>(AboutUsListView.java:39)
at com.example.myhospital.AboutUs.onCreate(AboutUs.java:46)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 15799 SIG: 9
这是关于我们 activity 的 Java 代码,它在列表视图中显示图像和一些数据。
AboutUs.java
package com.example.myhospital;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class AboutUs extends AppCompatActivity {
String[] name;
String[] email;
String[] imagepath;
ListView listView;
BufferedInputStream is;
String line=null;
String result=null;
String urladdress = "http://192.168.1.66/brs/team.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_us);
listView=(ListView)findViewById(R.id.lview);
StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
collectData();
AboutUsListView aboutUsListView=new AboutUsListView(this,name,email,imagepath);
listView.setAdapter(aboutUsListView);
}
private void collectData()
{
try{
URL url=new URL(urladdress);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
is=new BufferedInputStream(con.getInputStream());
}
catch (Exception ex)
{
ex.printStackTrace();
}
try{
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuilder sb=new StringBuilder();
while ((line=br.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
}
catch (Exception ex)
{
ex.printStackTrace();
}
try{
JSONArray ja=new JSONArray(result);
JSONObject jo=null;
name=new String[ja.length()];
email=new String[ja.length()];
imagepath=new String[ja.length()];
for(int i=0;i<=ja.length();i++){
jo=ja.getJSONObject(i);
name[i]=jo.getString("name");
email[i]=jo.getString("email");
imagepath[i]=jo.getString("photo");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
AboutUsListView.java
package com.example.myhospital;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.InputStream;
public class AboutUsListView extends ArrayAdapter<String> {
private String[] name;
private String[] email;
private String[] imagepath;
private Activity context;
Bitmap bitmap;
public AboutUsListView (Activity context, String[] name, String[] email, String[] imagepath) {
super(context, R.layout.layout, name);
this.context = context;
this.name = name;
this.email = email;
this.imagepath = imagepath;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
View r=convertView;
ViewHolder viewHolder=null;
if(r==null){
LayoutInflater layoutInflater=context.getLayoutInflater();
r=layoutInflater.inflate(R.layout.layout,null,true);
viewHolder=new ViewHolder(r);
r.setTag(viewHolder);
}
else {
viewHolder=(ViewHolder)r.getTag();
}
viewHolder.tvw1.setText(name[position]);
viewHolder.tvw2.setText(email[position]);
new GetImageFromURL(viewHolder.ivw).execute(imagepath[position]);
return r;
}
class ViewHolder{
TextView tvw1;
TextView tvw2;
ImageView ivw;
ViewHolder(View v){
tvw1=(TextView)v.findViewById(R.id.dname);
tvw2=(TextView)v.findViewById(R.id.dphone);
ivw=(ImageView)v.findViewById(R.id.imageView);
}
}
public class GetImageFromURL extends AsyncTask<String,Void, Bitmap>
{
ImageView imgView;
public GetImageFromURL(ImageView imgv)
{
this.imgView=imgv;
}
@Override
protected Bitmap doInBackground(String... url) {
String urldisplay = url[0];
bitmap = null;
try{
InputStream ist=new java.net.URL(urldisplay).openStream();
bitmap= BitmapFactory.decodeStream(ist);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap){
super.onPostExecute(bitmap);
imgView.setImageBitmap(bitmap);
}
}
}
这是关于我们页面的 XML 代码
AboutUs.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DFD8D8"
tools:context=".AboutUs">
<TextView
android:id="@+id/textView"
android:layout_width="418dp"
android:layout_height="62dp"
android:background="@color/red"
android:text="Available Ambulance Details"
android:textAlignment="center"
android:textColor="@android:color/background_light"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<ListView
android:id="@+id/lview"
android:layout_width="368dp"
android:layout_height="495dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.488"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.477" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的 XML 布局代码。
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#ffffff"
android:padding="15dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="86dp"
android:layout_weight="1" />
<TextView
android:id="@+id/dname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginTop="16dp"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:textColor="#333"
android:textSize="22dp"
/>
<TextView
android:id="@+id/dphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginTop="50dp"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:textColor="#333"
android:textSize="22dp"
/>
</RelativeLayout>
</LinearLayout>
您正在用空值初始化 AboutUsListView
Class。 ArrayAdapter
的构造不允许空值,如 documentation:
objects T: The objects to represent in the ListView. This value cannot be null.
您应该检查名称数组是否为空并将其替换为空数组:
public AboutUsListView (Activity context, String[] name, String[] email, String[] imagepath) {
super(context, R.layout.layout, name==null?new String[0]:name);
this.context = context;
this.name = name;
this.email = email;
this.imagepath = imagepath;
}