Android 工作室未显示 logcat 中的错误行号

Android studio is not showing the line number of error in logcat

我正在尝试将 firebase 实时数据库数据导入 ViewHolder class 但应用程序崩溃并且 logcat 没有指定行错误正在发生。下面是 logcat Logcat

2022-01-26 22:04:46.007 10834-11362/com.xee.fiverrimpressions E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #6
    Process: com.xee.fiverrimpressions, PID: 10834
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask.done(AsyncTask.java:415)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:305)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference
        at com.nguyencse.URLEmbeddedTask.doInBackground(URLEmbeddedTask.java:26)
        at com.nguyencse.URLEmbeddedTask.doInBackground(URLEmbeddedTask.java:13)
        at android.os.AsyncTask.call(AsyncTask.java:394)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:305) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:923) 
2022-01-26 22:04:46.097 10834-10834/com.xee.fiverrimpressions I/ViewRootImpl@9fa7fd8[DashboardActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
2022-01-26 22:04:46.108 10834-11362/com.xee.fiverrimpressions I/Process: Sending signal. PID: 10834 SIG: 9

MyViewHolder

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView geglink, userName, userAbout;
    URLEmbeddedView previewLink;
    View v;
    DatabaseReference dRef;
    FirebaseDatabase firebaseDatabase;
    FirebaseAuth fAuth;
    String UID;

    String fiverrLink1;

    public MyViewHolder(@NonNull @NotNull View itemView) {
        super(itemView);

        previewLink =  itemView.findViewById(R.id.uev);
        geglink = (TextView) itemView.findViewById(R.id.rcvGigLink);
        userName = (TextView) itemView.findViewById(R.id.rcvName);
        userAbout = (TextView) itemView.findViewById(R.id.rcvAbout);
        v = itemView;

        fAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();
        dRef = FirebaseDatabase.getInstance().getReference("Users");

        if(fAuth.getCurrentUser() != null){
            UID = FirebaseAuth.getInstance().getCurrentUser().getUid();

        }

     dRef.child(UID).addValueEventListener(new ValueEventListener() {
         @Override
         public void onDataChange(@NonNull DataSnapshot snapshot) {
             fiverrLink1 = (String) snapshot.child("gig").getValue();
         }

         @Override
         public void onCancelled(@NonNull DatabaseError error) {

         }
     });

        previewLink.setURL(fiverrLink1, new URLEmbeddedView.OnLoadURLListener() {
            @Override
            public void onLoadURLCompleted(URLEmbeddedData data) {
                previewLink.title(data.getTitle());
                previewLink.description(data.getDescription());
                previewLink.host(data.getHost());
                previewLink.thumbnail(data.getThumbnailURL());
                previewLink.favor(data.getFavorURL());
            }
        });

我认为错误在 addValueEventListener 好像我在外面写 fiverrLink1 = "https://www.youtube.com" addValueEventListener 应用程序完美运行。请帮助导致错误的原因以及解决方法。谢谢

崩溃发生在这里:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference
        at com.nguyencse.URLEmbeddedTask.doInBackground(URLEmbeddedTask.java:26)
        at com.nguyencse.URLEmbeddedTask.doInBackground(URLEmbeddedTask.java:13)

文件:URLEmbeddedTask.java 第 26 行

您在空字符串上调用了 startsWith(),后台任务崩溃了

例如,您可以添加一些日志消息并添加空检查:

if(tempString != null) {
    System.out.println("String is not null.. OK scenario")
    tempString.startsWith(/*aaaa*/);
} else {
    System.out.println("String is null.. NOK scenario")
}

更新

我找到了您在 GitHub 上使用的 class:

如您所见,在第 26 行,使用 url 时未检查它是否为空:

String url = params[0];
url = ((url.startsWith(URLConstants.PROTOCOL) || url.startsWith(URLConstants.PROTOCOL_S)) ? "" : URLConstants.PROTOCOL) + url;

因此,出现此问题是因为您使用空参数调用 task.execute();

URLEmbeddedTask task = new URLEmbeddedTask(/*listerner*/);
task.execute(/* THIS OBJECT CAN BE NULL */);

更新#2

我调试了那个库,发现错误是这样的:

String fiverrLink1;

dRef.child(UID).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        fiverrLink1 = (String) snapshot.child("gig").getValue();
        // Only here filverrLink1 wont be null. Only after this callback is invoked.
    }
});

// Here, fiverrLink1 is null. It will be non-null only after the onDataChanged call back is invoked.
previewLink.setURL(fiverrLink1 ... ); // -> You are sending null to the library

如何修复

好吧,这取决于您想要实现的目标。但我建议仅在 fiverrLink1 更新后调用 setUrl。类似于:

String fiverrLink1;

dRef.child(UID).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        fiverrLink1 = (String) snapshot.child("gig").getValue();
        previewLink.setURL(fiverrLink1 ... ); // -> Move to here
    }
});