我正在尝试从Firebase存储中获取图像并将其加载到导航标题上的“圆形图像视图”中。我用毕加索将文件设置为视图,但它重新调整为null ...我不知道为什么。
那是我得到的错误:
03-15 05:42:37.178 23532-23532/com.example.lnf E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lnf, PID: 23532
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lnf/com.example.lnf.MainActivity}: java.lang.IllegalArgumentException: Target must not be null.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:682)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:665)
at com.example.lnf.MainActivity.LoadImgToCircleView(MainActivity.java:97)
at com.example.lnf.MainActivity.onCreate(MainActivity.java:85)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
主要活动:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
private StorageReference profileImgRef;
private CircleImageView circleImageViewMain;
private TextView name, email;
String hearderImgUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initialize Firebase modules
firebaseAuth=FirebaseAuth.getInstance();
databaseReference= FirebaseDatabase.getInstance().getReference().child("users");
profileImgRef= FirebaseStorage.getInstance().getReference();
circleImageViewMain =findViewById(R.id.circleImageHeader);
name=findViewById(R.id.txtName);
email=findViewById(R.id.txtEmail);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
LoadImgToCircleView(CheckUserDataBase());
}
private void LoadImgToCircleView(String uid) {
try {
File localFile =File.createTempFile("profile","png");
StorageReference filepath=profileImgRef.child(uid).child("profileImg/profile.png");
filepath.getFile(localFile);
Picasso.get()
.load(localFile)
.placeholder(R.drawable.ic_account)
.into(circleImageViewMain);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser user=firebaseAuth.getCurrentUser();
if (user == null){
SendUserToLogin();
} else {
CheckUserDataBase();
}
}
private String CheckUserDataBase() {
final String userID =firebaseAuth.getCurrentUser().getUid();
databaseReference.addValueEventListener(
new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!dataSnapshot.child(userID).hasChild("userInfo")){
SendUserToSetup();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}
);
return userID;
}
private void SendUserToSetup() {
Intent i = new Intent(this,SetupActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
}
private void SendUserToLogin() {
Intent i = new Intent(this,LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
} else if (id == R.id.nav_profile) {
} else if (id == R.id.nav_my_posts) {
} else if (id == R.id.nav_messeges) {
} else if (id == R.id.action_settings) {
} else if (id == R.id.nav_logout) {
firebaseAuth.signOut();
SendUserToLogin();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
navigation_header_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/login"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/circleImageHeader"
android:layout_width="120dp"
android:layout_height="120dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@drawable/ic_account"
app:civ_border_color="@color/colorAccent"
app:civ_border_width="3dp" />
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nav_header_subtitle" />
build.gradle(应用程序)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.lnf"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
android.defaultConfig.vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.firebase:firebase-database:16.1.0'
implementation 'de.hdodenhof:circleimageview:3.0.0'
api 'com.theartofdev.edmodo:android-image-cropper:2.6.+'
implementation 'com.google.firebase:firebase-storage:16.1.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.firebaseui:firebase-ui-storage:4.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
答案 0 :(得分:0)
首先像这样更改活动文件中的初始化
circleImageViewMain =(CircleImageView)findViewById(R.id.circleImageHeader);
name=(TextView)findViewById(R.id.txtName);
email=(TextView)findViewById(R.id.txtEmail);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
告诉我什么错误日志后,
然后用picaso删除您的图像加载,替换为此
if(filepath.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(filepath.getAbsolutePath());
circleImageViewMain.setImageBitmap(myBitmap);
}
答案 1 :(得分:0)
您的图片url首先来自Firebase还是不是来自Firebase,
//Nav Header Section
//Here I am using glide you can use picasso
View header = navigationView.getHeaderView(0);
navImageView = header.findViewById(R.id.nav_ImageView);
navTextView = header.findViewById(R.id.nav_Textview);
navTextView.setText(sharedPreferences.getString("name", ""));
Log.d("success121", String.valueOf(navTextView));
navText = sharedPreferences.getString("logo",null);
if(navText!=null) {
Log.e("Photo Url: ", navText);
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_launcher_background);
requestOptions.error(R.drawable.ic_launcher_foreground);
Glide.with(this).load(navText)
.apply(requestOptions).thumbnail(0.5f).into(navImageView);
}
,在您的代码中,您并不是对编译器设置图像的地方说。 根据您的代码,编译器尝试在活动而不是导航标题上设置图像。