gpt4 book ai didi

java - 开始 Activity 未开始

转载 作者:行者123 更新时间:2023-12-02 09:10:45 25 4
gpt4 key购买 nike

我的 Activity 启动 Intent 适用于其他 Activity ,但我无法让它在这个特定的 Activity 上工作(PostActivity.java)。 以下是以下类:

Activity 后:


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shrinkio.Fragments.HomeFragment;
import com.example.shrinkio.MainActivities.BottomActivity;
import com.example.shrinkio.R;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.StorageTask;
import com.theartofdev.edmodo.cropper.CropImage;

import java.util.HashMap;
import java.util.Objects;

public class PostActivity extends AppCompatActivity {

ImageButton video;
StorageTask uploadTask;
EditText Post;
Uri imageUri;
String myUrl = "";
ImageView close;

private static final int GALLERY_REQUEST = 1;

FirebaseAuth firebaseAuth;
FirebaseUser firebaseUser;
DatabaseReference databaseReference;
StorageReference storageReference;
TextView post;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);

Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout_post);

Post = findViewById(R.id.Post);

close = findViewById(R.id.close);
post = findViewById(R.id.post);
storageReference = FirebaseStorage.getInstance().getReference("post");


close.setOnClickListener(view -> startActivity(new Intent(PostActivity.this, BottomActivity.class)));
finish();

post.setOnClickListener(view -> uploadImage());


video = findViewById(R.id.ImageButton);
video.setOnClickListener(view -> {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);

});


}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.post_menu, menu);
return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
{
if (item.getItemId() == R.menu.post_menu){

FirebaseDatabase.getInstance().getReference().child("Users").child("post").push();

String post = Post.getText().toString();


startActivity(new Intent(PostActivity.this, BottomActivity.class));
Toast.makeText(PostActivity.this, "Shrinked", Toast.LENGTH_LONG).show();
overridePendingTransition(0,0);


video.setImageURI(imageUri);

DatabaseReference nPost = databaseReference.push();
nPost.child("post").setValue(post);
nPost.child("img").setValue(imageUri);

}
}
return super.onOptionsItemSelected(item);
}


//Post




private String getFileExtension(Uri uri){
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(contentResolver.getType(uri));

}


private void uploadImage(){
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Posting...");

if (imageUri != null) {
StorageReference filereference = storageReference.child(System.currentTimeMillis()+ "." + getFileExtension(imageUri));
uploadTask = filereference.putFile(imageUri);

uploadTask.continueWithTask((Continuation) task -> {
if (!task.isSuccessful()) {
throw Objects.requireNonNull(task.getException());
}

return filereference;
})
.addOnCompleteListener((OnCompleteListener<Uri>) task -> {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
myUrl = downloadUri.toString();

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
String postId = reference.push().getKey();

HashMap<String , Object> hashMap = new HashMap<>();
hashMap.put("postId", post);
hashMap.put("post_image", myUrl);
hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());

reference.child(postId).setValue(hashMap);
progressDialog.dismiss();
startActivity(new Intent(PostActivity.this, BottomActivity.class));
finish();
} else {
Toast.makeText(PostActivity.this, "Post failed!", Toast.LENGTH_SHORT).show();

} }).addOnFailureListener(e -> Toast.makeText(PostActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show());

} else {
Toast.makeText(PostActivity.this, " No Image Selected!", Toast.LENGTH_SHORT).show();
}
}




@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){

CropImage.ActivityResult result = CropImage.getActivityResult(data);
assert result != null;
imageUri = result.getUri();
video.setImageURI(imageUri);
video.setImageURI(imageUri);
}
else {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, BottomActivity.class));
finish();
}
}
}

这是 Intent PostActivity.java 的 Activity

BottomActivity.java:


import android.annotation.SuppressLint;
import android.content.Intent;
import android.gesture.GestureOverlayView;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toolbar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.multidex.MultiDex;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.shrinkio.Fragments.HomeFragment;
import com.example.shrinkio.Fragments.MessagesFragment;
import com.example.shrinkio.Fragments.NotFragment;
import com.example.shrinkio.Fragments.PeopleFragment;
import com.example.shrinkio.R;
import com.example.shrinkio.SecondaryActivities.PostActivity;
import com.example.shrinkio.SecondaryActivities.Posts;
import com.example.shrinkio.SecondaryActivities.ProfileActivity;
import com.example.shrinkio.SecondaryActivities.SettingsActivity;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.Objects;

import static com.example.shrinkio.R.menu.main_menu;


public class BottomActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {



RecyclerView rv;
TextView post_desc;
FirebaseDatabase mDatabase;
DatabaseReference myRef;
private static final String TAG = "";
Fragment selectedFragment = null;

@SuppressLint({"RtlHardcoded", "ClickableViewAccessibility"})
@Override
protected void onCreate(Bundle savedInstanceState) {
MultiDex.install(this);
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_bottom );
new GestureOverlayView( this );

Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout_home);




//Action bar configurations


getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit() ;

BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation1);
bottomNav.setOnNavigationItemSelectedListener(navListener);


rv = findViewById(R.id.recyclerView);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));

post_desc = findViewById(R.id.post_desc);
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

mDatabase = FirebaseDatabase.getInstance();
String user_id = firebaseUser.getUid();
myRef = mDatabase.getReference().child("Users").child(user_id).child("post");






}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_post:

Intent intent = new Intent(this, PostActivity.class);
startActivity(intent);
overridePendingTransition(0,0);
break;

}
return super.onOptionsItemSelected(item);
}




private boolean loadFragment(Fragment fragment) {
if ( fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
}
return loadFragment(new HomeFragment());

}
@Override
protected void onStart() {
super.onStart();

Query query = myRef.child("post");
FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(query, Posts.class)
.build();


FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter <Posts, PostViewHolder> (
options)
{
@Override
protected void onBindViewHolder(@NonNull PostViewHolder holder, int position, Posts model) {
holder.setPost(model.getPost());

}

@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.posts, parent, false);
return new PostViewHolder(view);

}
};


rv.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();




}
public static class PostViewHolder extends RecyclerView.ViewHolder{
View mView;

public PostViewHolder(@NonNull View itemView) {
super(itemView);

mView = itemView;
}

public void setPost(String post) {
TextView post_title = mView.findViewById(R.id.post_desc);
post_title.setText(post);
Log.d(TAG, "It works");
}
}







BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

switch (menuItem.getItemId()) {
case R.id.navigation_home:
selectedFragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
selectedFragment = new NotFragment();
break;
case R.id.navigation_people:
selectedFragment = new PeopleFragment();
break;
case R.id.navigation_messages:
selectedFragment = new MessagesFragment();
}

if (selectedFragment != null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
}
return true;
}

};

@Override
public void onBackPressed() {
super.onBackPressed();
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_home:
selectedFragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
selectedFragment = new NotFragment();
break;
case R.id.navigation_people:
selectedFragment = new PeopleFragment();
break;
case R.id.navigation_messages:
selectedFragment = new MessagesFragment();
}

if (selectedFragment != null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
}

return true;
}
}







我正在调用菜单项单击上的 Intent ,但没有任何反应,logcat 上也没有显示错误,但是,对于其他 Activity ,它就像一个魅力。

我已经尝试了 StackOverflow 上的每个问题,但似乎都不起作用,感谢任何帮助。

最佳答案

也许finish()是这种情况? onCreate() 中的 PostActivity 包含 finish()

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);

Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout_post);

Post = findViewById(R.id.Post);

close = findViewById(R.id.close);
post = findViewById(R.id.post);
storageReference = FirebaseStorage.getInstance().getReference("post");

close.setOnClickListener(view -> startActivity(new Intent(PostActivity.this, BottomActivity.class)));
// TODO remove this one
finish();

post.setOnClickListener(view -> uploadImage());


video = findViewById(R.id.ImageButton);
video.setOnClickListener(view -> {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);

});
}

关于java - 开始 Activity 未开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59440998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com