首页 > 解决方案 > 为什么我的 android 应用程序因新意图而崩溃,而没有任何错误日志?

问题描述

背景 - 我的应用程序使用 firebase,它崩溃的意图是 TabHost 的一部分。

设置标签:

        th=(TabHost) findViewById(R.id.tabHost);
        LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
        mLocalActivityManager.dispatchCreate(savedInstanceState);
        th.setup(mLocalActivityManager);
        TabHost.TabSpec tabSpec = th.newTabSpec("tab1");
        tabSpec.setIndicator("Users");
        Intent intent = new Intent(this, usersActivity.class);
        tabSpec.setContent(intent);

        TabHost.TabSpec tabSpec2 = th.newTabSpec("tab2");
        tabSpec2.setIndicator("Send Message");
        Intent intent2 = new Intent(activityTwo.this, messageActivity.class);
        //startActivityForResult(intent2,100);
        tabSpec2.setContent(intent2);

        TabHost.TabSpec tabSpec3 = th.newTabSpec("tab3");
        tabSpec3.setIndicator("Group Chats");
        Intent intent3 = new Intent(activityTwo.this, chatActivity.class);
        tabSpec3.setContent(intent3);

        th.addTab(tabSpec);
        th.addTab(tabSpec2);
        th.addTab(tabSpec3);

标签 1 和 2 很好。单击选项卡 3 会使我的应用程序崩溃。chatActivity 类如下所示:

public class chatActivity extends AppCompatActivity {

    File localFile = null;
    FirebaseUser fUser;
    File[] fList=null;
    LinearLayout imageGallery;
    ImageView imageView;
    ArrayList<Integer> alI = new ArrayList<Integer>();
    DatabaseReference dbRef = galleryActivity.sendImgRef();
    String changeNotifName="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        System.out.println("Entered chat activity"); //SYSOUT HERE
        imageGallery = (LinearLayout) findViewById(R.id.imageGallery);
        StorageReference sr1 = FirebaseStorage.getInstance().getReference();
        final StorageReference sr2 = sr1.child("images");
        System.out.println("Just before value check change"); //ANOTHER ONE HERE
        dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot dsp : dataSnapshot.getChildren())
                {
                    changeNotifName=String.valueOf(dsp.getValue());
                    System.out.println("changed name: "+changeNotifName);
                    sr2.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
                        @Override
                        public void onSuccess(ListResult listResult) {
                            try {
                                localFile = File.createTempFile("images", "jpg");
                                System.out.println("Before size: "+localFile.length());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            for (StorageReference prefix : listResult.getPrefixes()) {

                            }

                            for (StorageReference item : listResult.getItems()) {
                                String s[] = item.getName().split(".");
                                System.out.println("itemName: "+s[0]);
                                if(changeNotifName.indexOf(s[0])!=-1)
                                {
                                    item.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                                        @Override
                                        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                                            save();
                                            addImagesToThegallery();
                                            System.out.println("local File Size: "+localFile.length());
                                        }
                                    }).addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception exception) {
                                            // Handle any errors
                                        }
                                    });
                                }
                            }
                        }
                    })
                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    // Uh-oh, an error occurred!
                                }
                            });
                }
            }

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

            }
        });

        addImagesToThegallery();
    }

    public void save()
    {
        File f = localFile;
        fUser = FirebaseAuth.getInstance().getCurrentUser();
        String tmp[] = fUser.getEmail().split("@");
        String name = tmp[0];
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmsss").format(new Date());
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        if (!path.isDirectory()) {
            path.mkdirs();
        }
        Bitmap bmp = BitmapFactory.decodeFile(localFile.getAbsolutePath());
        File imageFile = new File(path, name + timeStamp + ".jpg");
        try {
            FileOutputStream out = new FileOutputStream(imageFile);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            System.out.println("CANT BELIEVE IT WORKED!");
        }
        catch (IOException ioe) {ioe.printStackTrace();}

        MediaScannerConnection.scanFile(getApplicationContext(), new String[]{imageFile.getAbsolutePath()}, null, new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    }

    private void addImagesToThegallery() {

        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        fList = path.listFiles();
        if(fList!=null)
        {
            int len = fList.length;
            for(int i=0; i<len; i++)
            {
                imageGallery.addView(getImageView(i));
            }
        }
        else
        {
            Toast.makeText(this, "fList is null!", Toast.LENGTH_SHORT).show();
        }

    }

    private View getImageView(int image) {
        imageView = new ImageView(getApplicationContext());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 10, 0);
        imageView.setLayoutParams(lp);
        Bitmap bitmap = BitmapFactory.decodeFile(fList[image].getAbsolutePath());
        alI.add(image);
        imageView.setImageBitmap(bitmap);

        return imageView;
    }
}

Logcat 没有显示错误,但我的应用程序仍然崩溃!没有执行任何 sysout 语句。编译器甚至没有到达那部分。可能是什么问题?

标签: androidfirebaseandroid-tabhost

解决方案


推荐阅读