有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ImageView为空

我在活动中遇到了一个关于ImageView的小问题。我正在尝试使用毕加索将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 安卓.widget.ImageView.setImageResource(int)' on a null object reference
                  at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                  at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                  at 安卓.app.ActivityThread.-wrap14(ActivityThread.java)
                  at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                  at 安卓.os.Handler.dispatchMessage(Handler.java:102)
                  at 安卓.os.Looper.loop(Looper.java:154)
                  at 安卓.app.ActivityThread.main(ActivityThread.java:6776)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                  at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.widget.ImageView.setImageResource(int)' on a null object reference
                  at com.example.pd13user.hltvmatches.MatchDetailActivity.onCreate(MatchDetailActivity.java:65)
                  at 安卓.app.Activity.performCreate(Activity.java:6955)
                  at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                  at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                  at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                  at 安卓.app.ActivityThread.-wrap14(ActivityThread.java) 
                  at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                  at 安卓.os.Handler.dispatchMessage(Handler.java:102) 
                  at 安卓.os.Looper.loop(Looper.java:154) 
                  at 安卓.app.ActivityThread.main(ActivityThread.java:6776) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                  at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

添加后

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

似乎两个ImageView都为空。这是我的密码:

    package com.example.pd13user.hltvmatches;

import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.support.design.widget.FloatingActionButton;
import 安卓.support.design.widget.Snackbar;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.Toolbar;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.widget.ImageView;
import 安卓.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]);
    }

}

我尝试了findviewbyd,但没有膨胀视图(这对于我的文本视图来说很好,因为它们在没有任何异常的情况下正确显示),这会产生相同的异常

编辑:这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓: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
        安卓:id="@+id/teamLogo1"
        安卓:layout_width="50dp"
        安卓:layout_height="50dp"
        安卓:layout_marginBottom="8dp"
        安卓:layout_marginEnd="8dp"
        安卓:layout_marginStart="8dp"
        安卓: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
        安卓:id="@+id/teamLogo2"
        安卓:layout_width="50dp"
        安卓:layout_height="50dp"
        安卓:layout_marginEnd="8dp"
        安卓:layout_marginStart="8dp"
        安卓: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
        安卓:id="@+id/team1"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="8dp"
        安卓:layout_marginEnd="8dp"
        安卓:layout_marginStart="8dp"
        安卓:text="TextView"
        安卓: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
        安卓:id="@+id/vs"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="8dp"
        安卓:layout_marginEnd="8dp"
        安卓:layout_marginStart="8dp"
        安卓:layout_marginTop="8dp"
        安卓:text="@string/versus"
        安卓: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
        安卓:id="@+id/team2"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="8dp"
        安卓:layout_marginEnd="8dp"
        安卓:layout_marginStart="8dp"
        安卓:layout_marginTop="8dp"
        安卓:text="TextView"
        安卓: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
        安卓:id="@+id/time"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="TextView"
        安卓: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
        安卓:id="@+id/event"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="TextView"
        安卓:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/time" />

</安卓.support.constraint.ConstraintLayout>

共 (5) 个答案

  1. # 1 楼答案

    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中,您没有使用I'd property,但在Java中,您使用的是teamLogo1,teamlogo2也是如此

  2. # 2 楼答案

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

  3. # 3 楼答案

    您正在显示xml{},因此xml显示在activity_match_detail内部,因此您不需要inflate显示layout

    只是作为普通元素使用

    因此,替换为以下代码

    teamLogo1 = (ImageView) findViewById(R.id.teamlogo1);
    teamLogo2 = (ImageView) findViewById(R.id.teamlogo2);
    
  4. # 4 楼答案

    哎哟。打字错误。我现在觉得自己很笨

    我使用带有小l的teamlogo1,而不是带有大写l的teamlogo1

  5. # 5 楼答案

    java文件上正确使用id。它应该与您在layoutxml文件中编写的内容相同。您使用了v.findViewById(R.id.teamlogo1);而不是teamLogo1。若布局xml文件和您在setContentView()方法中提供的相同,那个么删除膨胀视图,只需使用findViewById(R.id.teamLogo1)