ImageView为null

时间:2017-11-28 13:08:40

标签: java android nullpointerexception imageview

我的Activity中有一个关于ImageView的问题。我正在尝试使用Picasso将teamLogo1和teamLogo2设置为徽标。发生以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.pd13user.hltvmatches, PID: 28179
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pd13user.hltvmatches/com.example.pd13user.hltvmatches.MatchDetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                  at android.app.ActivityThread.-wrap14(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6776)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
                  at com.example.pd13user.hltvmatches.MatchDetailActivity.onCreate(MatchDetailActivity.java:65)
                  at android.app.Activity.performCreate(Activity.java:6955)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                  at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6776) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

添加

if(teamLogo1 != null && teamLogo2 != null){...}

似乎两个ImageView都是null。这是我的代码:

    package com.example.pd13user.hltvmatches;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class MatchDetailActivity extends AppCompatActivity {

    Intent i = getIntent();
    ImageView teamLogo1;
    ImageView teamLogo2;
    TextView team1;
    TextView team2;
    TextView time;
    TextView event;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setTitle(getIntent().getStringExtra("date"));
        setContentView(R.layout.activity_match_detail);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.content_match_detail, null);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) 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();
            }
        });


        teamLogo1 = (ImageView) v.findViewById(R.id.teamlogo1);
        teamLogo2 = (ImageView) v.findViewById(R.id.teamlogo2);
        team1 = (TextView) findViewById(R.id.team1);
        team2 = (TextView) findViewById(R.id.team2);
        time = (TextView) findViewById(R.id.time);
        event = (TextView) findViewById(R.id.event);

        String[] data = getIntent().getStringArrayExtra("data");

        /*if(teamLogo1 != null && teamLogo2 != null) {
            Picasso.with(this).load(data[4]).resize(50, 50).into(teamLogo1);
            Picasso.with(this).load(data[5]).resize(50, 50).into(teamLogo2);
        }*/

        teamLogo1.setImageResource(R.drawable.test);
        teamLogo2.setImageResource(R.drawable.test);

        System.out.println(data[4] + "\n" + data[5]);

        team1.setText(data[1]);
        team2.setText(data[2]);
        time.setText(data[0]);
        event.setText(data[3]);
    }

}

我已经尝试了findViewById而没有给视图充气(这对我的TextViews来说效果很好,因为它们没有任何例外地正确显示)会产生相同的异常。

编辑:这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.pd13user.hltvmatches.MatchDetailActivity"
    tools:layout_editor_absoluteY="137dp"
    tools:showIn="@layout/activity_match_detail">

    <ImageView
        android:id="@+id/teamLogo1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/team1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_menu_slideshow" />

    <ImageView
        android:id="@+id/teamLogo2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/time"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/team2"
        app:srcCompat="@drawable/ic_menu_send" />

    <TextView
        android:id="@+id/team1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="TextView"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="@+id/vs"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/teamLogo1" />

    <TextView
        android:id="@+id/vs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/versus"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/team2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/team1" />

    <TextView
        android:id="@+id/team2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="@+id/teamLogo2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/vs" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/event"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/teamLogo2" />

    <TextView
        android:id="@+id/event"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/time" />

</android.support.constraint.ConstraintLayout>

5 个答案:

答案 0 :(得分:1)

id文件上正确使用java。它应该与您在布局xml文件上编写的相同。您使用v.findViewById(R.id.teamlogo1);代替teamLogo1。如果layout xml文件与您在setContentView()方法中提供的相同,则删除膨胀视图,只需使用findViewById(R.id.teamLogo1)

答案 1 :(得分:1)

哎哟。一个错字。我现在感到愚蠢。

我使用了teamlogo1和一个小l而不是teamLogo1和一个大写L.

答案 2 :(得分:0)

v

开头删除v.findViewById(R.id.teamlogo1);

答案 3 :(得分:0)

teamLogo1 = (ImageView) v.findViewById(R.id.teamlogo1); 
teamLogo2 = (ImageView) v.findViewById(R.id.teamlogo2);

将以下两行替换为以下两行。

teamLogo1 = (ImageView) findViewById(R.id.teamLogo1); 
teamLogo2 = (ImageView) findViewById(R.id.teamLogo2);

你也没有在XML teamLogo1中使用我,但在Java中你使用的是teamlogo1,而teamlogo2也是如此。

答案 4 :(得分:0)

您正在显示xml Showin,以便在xml内显示activity_match_detail,因此您无需inflate layout < / p>

只需使用Normal元素即可。

所以请用以下代码替换。

teamLogo1 = (ImageView) findViewById(R.id.teamlogo1);
teamLogo2 = (ImageView) findViewById(R.id.teamlogo2);