【发布时间】:2019-08-16 21:31:05
【问题描述】:
我尝试使用意图更改视图 在我的代码中,同样的句子有效,但在另一个活动中它不起作用
nav_top_post = view.findViewById(R.id.nav_top_post);
nav_top_post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(HomeFragment.this, PostActivity.class));
}
});
在这部分显示红线并显示消息 '无法解析构造函数意图'
我找不到这是怎么回事
-
这是我的 frament_home.xml *
<android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bar" android:background="?android:attr/windowBackground" > <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="3dp"> <ImageButton android:id="@+id/nav_top_post" android:layout_width="50dp" android:layout_height="45dp" android:layout_alignParentStart="true" android:layout_marginStart="5dp" android:src="@drawable/ic_top_post_add" /> <ImageView android:layout_width="200dp" android:layout_height="45dp" android:layout_centerInParent="true" android:src="@drawable/together_logo" /> <ImageButton android:layout_width="50dp" android:layout_height="45dp" android:src="@drawable/ic_top_message" android:layout_marginRight="5dp" android:layout_alignParentEnd="true"/> </RelativeLayout> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/bar" android:id="@+id/recycler_view"> </android.support.v7.widget.RecyclerView> -
这是我的 HomeFragment.class *
包 com.example.blogapp.Fragment;
导入 android.content.Context; 导入android.content.Intent; 导入android.net.Uri; 导入android.os.Bundle; 导入android.support.v4.app.Fragment; 导入 android.view.LayoutInflater; 导入android.view.View; 导入android.view.ViewGroup; 导入android.widget.ImageButton;
导入 com.example.blogapp.Activities.HomeActivity; 导入 com.example.blogapp.Activities.PostActivity; 导入 com.example.blogapp.R;
公共类 HomeFragment 扩展 Fragment {
ImageButton nav_top_post; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home, container, false); nav_top_post = view.findViewById(R.id.nav_top_post); nav_top_post.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(HomeFragment.this, PostActivity.class)); } }); // Inflate the layout for this fragment return view; }}
- 我想使用 Intent 方法移动 PostActivity.java *
包 com.example.blogapp.Activities;
导入android.app.Fragment; 导入 android.content.ContentResolver; 导入android.content.Intent; 导入android.net.Uri; 导入android.support.annotation.NonNull; 导入 android.support.annotation.Nullable; 导入 android.support.design.widget.BottomNavigationView; 导入android.support.v7.app.AppCompatActivity; 导入android.os.Bundle; 导入android.view.View; 导入 android.webkit.MimeTypeMap; 导入 android.widget.EditText; 导入android.widget.ImageView; 导入 android.widget.TextView; 导入android.widget.Toast;
导入 com.example.blogapp.R; 导入 com.google.android.gms.tasks.Continuation; 导入 com.google.android.gms.tasks.OnCompleteListener; 导入 com.google.android.gms.tasks.OnFailureListener; 导入 com.google.android.gms.tasks.Task; 导入 com.google.firebase.auth.FirebaseAuth; 导入 com.google.firebase.database.DatabaseReference; 导入 com.google.firebase.database.FirebaseDatabase; 导入 com.google.firebase.storage.FirebaseStorage; 导入 com.google.firebase.storage.StorageReference; 导入 com.google.firebase.storage.StorageTask; 导入 com.theartofdev.edmodo.cropper.CropImage;
导入 java.util.HashMap;
公共类 PostActivity 扩展 AppCompatActivity {
Uri imageUri;
String myUrl = "";
StorageTask uploadTask;
StorageReference storageReference;
ImageView close, image_added;
TextView post;
EditText description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
close = findViewById(R.id.close);
image_added = findViewById(R.id.image_added);
post = findViewById(R.id.post);
description = findViewById(R.id.description);
storageReference = FirebaseStorage.getInstance().getReference("posts");
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(PostActivity.this, HomeActivity.class));
finish();
}
});
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
}
});
CropImage.activity()
.setAspectRatio(1, 1)
.start(PostActivity.this);
}
private String getFileExtension(Uri uri){
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(contentResolver.getType(uri));
}
private void uploadImage(){
if(imageUri!= null){
final StorageReference filereference = storageReference.child(System.currentTimeMillis() + "."+getFileExtension(imageUri));
uploadTask = filereference.putFile(imageUri);
uploadTask.continueWith(new Continuation() {
@Override
public Object then(@NonNull Task task) throws Exception {
if(!task.isComplete()){
throw task.getException();
}
return filereference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful()){
Uri downloadUri = task.getResult();
myUrl = downloadUri.toString();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
String postid = reference.push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("postid", postid);
hashMap.put("postimage", myUrl);
hashMap.put("description", description.getText().toString());
hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.child(postid).setValue(hashMap);
startActivity(new Intent(PostActivity.this, HomeActivity.class));
finish();;
}else {
showMessage("업로드에 실패하였습니다!");
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
showMessage(e.getMessage());
}
});
} else{
showMessage("이미지를 골라주세요!");
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
imageUri = result.getUri();
image_added.setImageURI(imageUri);
}else{
showMessage("에러가 발생했습니다.");
startActivity(new Intent(PostActivity.this, HomeActivity.class));
finish();
}
}
private void showMessage(String text) {
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
}
}
* This sentense working in another my class *
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(PostActivity.this, HomeActivity.class));
finish();
}
});
是什么原因?
我希望知道显示错误信息的原因是什么
【问题讨论】:
标签: android android-intent imagebutton