背景-我的应用使用了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语句被执行。编译器甚至没有达到那部分。可能是什么问题?