0

如何修复此代码?由于这些原因,它在 logcat 中不断关闭应用程序?

我得到的错误在这里

每次我运行它时它都会崩溃或停止。我没有构建错误,但我检查了我的 logcat 的原因,但仍然无法解决它。请发送帮助错误:如果需要更清晰,请参见图片

2021-04-26 04:31:41.006 29414-29414/com.mobdeve.santosm.androidchallenge E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mobdeve.santosm.androidchallenge, PID: 29414
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobdeve.santosm.androidchallenge/com.mobdeve.santosm.androidchallenge.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3123)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3266)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7099)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:388)
        at android.widget.ListView.setAdapter(ListView.java:614)
        at com.mobdeve.santosm.androidchallenge.MainActivity.onCreate(MainActivity.java:29)
        at android.app.Activity.performCreate(Activity.java:7327)
        at android.app.Activity.performCreate(Activity.java:7318)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3266) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7099) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 

这是我的代码

主要活动:

import androidx.appcompat.app.AppCompatActivity;

   import android.content.Intent;
   import android.os.Bundle;
   import android.util.Log;
   import android.view.View;
   import android.widget.Button;
   import android.widget.ListView;

   import java.util.ArrayList;

   public class MainActivity extends AppCompatActivity {

    private Button newEmail;
    private ListView listView;
    private EmailAdapter emailAdapter;
    private ArrayList<Emails> emailsArrayList;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();


         emailAdapter = new EmailAdapter(this, emailsArrayList);
         listView.setAdapter(emailAdapter);


        newEmail.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SendActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onStart() {
         super.onStart();
         Log.d("MainActivity","onStart invoked");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("MainActivity","onResume invoked");
    }

    @Override
    protected void onPause() {

        super.onPause();
        Log.d("MainActivity","onPause invoked");
    }

    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    protected void onRestart() {

        super.onRestart();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    private void init(){
        newEmail = (Button) findViewById(R.id.newBtn);
        listView = (ListView) findViewById(R.id.list);
    }
}

适配器:

package com.mobdeve.santosm.androidchallenge;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


import java.util.ArrayList;

public class EmailAdapter extends ArrayAdapter<Emails>{

    private Activity activity;

    public EmailAdapter(Activity activity, ArrayList<Emails> emailsArrayList){
        super(activity, R.layout.emails_row, emailsArrayList);
        this.activity = activity;
    }

    @Override
    public View getView(int position,View convertView, ViewGroup parent) {
        View rowView = convertView;


        if (rowView == null){

            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.emails_row, null);


            ViewHolder emailsViewHolder = new ViewHolder();
            emailsViewHolder.rEmail= (TextView) rowView.findViewById(R.id.rEmail);
            emailsViewHolder.rBody= (TextView) rowView.findViewById(R.id.rBody);
            emailsViewHolder.rSubject= (TextView) rowView.findViewById(R.id.rSubject);

            rowView.setTag(emailsViewHolder);
        }

        final ViewHolder holder = (ViewHolder) rowView.getTag();
        Emails info = getItem(position);
        holder.rEmail.setText(info.getEmails());
        holder.rBody.setText(info.getBody());
        holder.rSubject.setText(info.getSubject());

        return rowView;
    }

    static class ViewHolder{
        public TextView rEmail;
        public TextView rBody;
        public TextView rSubject;
    }
}
4

1 回答 1

0

只需将以下行添加到您的init()方法中:

emailsArrayList = new ArrayList<>();

那应该解决NullPointerException

于 2021-04-25T20:54:31.290 回答