java.lang.NullPointerException:尝试调用虚拟方法android studio [重复]

Posted

技术标签:

【中文标题】java.lang.NullPointerException:尝试调用虚拟方法android studio [重复]【英文标题】:java.lang.NullPointerException: Attempt to invoke virtual method android studio [duplicate] 【发布时间】:2020-05-05 04:48:54 【问题描述】:

我的 android studio 有问题,我想用包裹传递数据,但是如果我单击列表视图总是强制关闭,它不能导致我收到此消息,如何解决?

错误


    package com.example.moviee;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class MovieDetail extends AppCompatActivity 

    String txt_name;
    String txt_deskripsi;
    int foto;

    public static final String EXTRA_MOVIE = "text_extra_movie";

    TextView tvName, txt_detail;
    ImageView imgposter;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_detail);

        tvName = findViewById(R.id.text_ph);
        txt_detail = findViewById(R.id.txt_deksrip);
        imgposter = findViewById(R.id.img_poster);

        ArrayList<Film> film = new ArrayList<>();
        film = this.getIntent().getParcelableArrayListExtra(EXTRA_MOVIE);

        txt_name = film.get(0).getNama();
        tvName.setText(txt_name);

        txt_deskripsi = film.get(0).getDeskripsi();
        txt_detail.setText(txt_deskripsi);

        foto = film.get(0).getFilm();
        imgposter.setImageResource(foto);
    

movieDetail.java

    package com.example.moviee;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity implements View.OnClickListener 
    private FilmAdapter adapter;
    private String[] dataNama;
    private String[] dataDeskrip;
    private TypedArray dataPhoto;
    private ArrayList<Film> filmed;

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


        adapter = new FilmAdapter(this);
        ListView listView = findViewById(R.id.lv_list);
        listView.setAdapter(adapter);

        prepare();
        addItem();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
                Intent intent = new Intent(MainActivity.this, MovieDetail.class);
                ArrayList<Film> movies = new ArrayList<Film>();

                Film movie = new Film();
                movie.setFilm(dataPhoto.getResourceId(i, -1));
                movie.setNama(dataNama[i]);
                movie.setDeskripsi(dataDeskrip[i]);


                movies.add(movie);
                intent.putParcelableArrayListExtra(MovieDetail.EXTRA_MOVIE, movies);
                startActivity(intent);
            
        );
    

    @Override
    public void onClick(View v) 
    
    private void addItem()
        ArrayList<Film> films = new ArrayList<>();

        for (int i = 0; i < dataNama.length; i++)
            Film film = new Film();
            film.setFilm(dataPhoto.getResourceId(i, -1));
            film.setNama(dataNama[i]);
            film.setDeskripsi(dataDeskrip[i]);
            films.add(film);
        
        adapter.setFilms(films);
    


    private void prepare()
        dataNama = getResources().getStringArray(R.array.data_film);
        dataDeskrip = getResources().getStringArray(R.array.data_deskripsi);
        dataPhoto = getResources().obtainTypedArray(R.array.data_foto);
    

mainactivity.java

【问题讨论】:

【参考方案1】:

您需要先初始化ArrayList&lt;&gt;

ArrayList<Film> film = new ArrayList<>();
film = this.getIntent().getParcelableArrayListExtra(EXTRA_MOVIE);

然后你就可以访问它的值了

txt_name = film.get(0).getName();
tvName.setText(txt_name);

【讨论】:

它不能有同样的问题,如何? 粘贴有问题的完整 logcat 跟踪 完成我把它放在新评论中

以上是关于java.lang.NullPointerException:尝试调用虚拟方法android studio [重复]的主要内容,如果未能解决你的问题,请参考以下文章

亲測,Eclipse报&quot;An error has occurred,See error log for more details. java.lang.NullPointerExce

来自 LayerDrawable 的异常

Microsoft Access 和 Java JDBC-ODBC 错误

如何使用其id获取单个SQLite行数据?