У меня проблема при передаче данных из основного действия в другое действие с использованием Intents
.
Я сделал MainActivity
, BaiHat class
и SecondActivity
для получения данных из MainActivity
.
Открыть logcat, есть такая ошибка:
(java .lang.NullPointerException: попытка вызвать виртуальный метод 'java .lang.String java .lang.Object.toString ()' для ссылки на пустой объект в com.myappnvt.truyendulieu.MainActivity $ 1.onItemClick (MainActivity. java: 30)) MainActivity. java: 30 - это строка "String ten_bai_hat = listView.getItemAtPosition (position) .toString ();"
Вот код:
package com.myappnvt.truyendulieu;
import android.os.Parcel;
import android.os.Parcelable;
import java.lang.reflect.Array;
public class BaiHat implements Parcelable {
private String tenBaiHat;
private int tenFile;
public BaiHat(String tenBaiHat, int tenFile) {
this.tenBaiHat = tenBaiHat;
this.tenFile = tenFile;
}
public BaiHat(Parcel in) {
tenBaiHat = in.readString();
tenFile = in.readInt();
}
public static final Creator<BaiHat> CREATOR = new Creator<BaiHat>() {
@Override
public BaiHat createFromParcel(Parcel in) {
return new BaiHat(in);
}
@Override
public BaiHat[] newArray(int size) {
return new BaiHat[size];
}
};
public String getTenBaiHat() {
return tenBaiHat;
}
public void setTenBaiHat(String tenBaiHat) {
this.tenBaiHat = tenBaiHat;
}
public int getTenFile() {
return tenFile;
}
public void setTenFile(int tenFile) {
this.tenFile = tenFile;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(tenBaiHat);
dest.writeInt(tenFile);
}
public static final Parcelable.Creator<BaiHat> creator = new Parcelable.Creator<BaiHat>(){
public BaiHat createFromParcel(Parcel in){
return new BaiHat(in);
}
public BaiHat[] newArray (int size){
return new BaiHat[size];
}
};
}
package com.myappnvt.truyendulieu;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
BaiHatAdapter adapter;
ListView listView;
ArrayList<BaiHat> baiHatArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Addlist();
adapter = new BaiHatAdapter(MainActivity.this, R.layout.dong_bai_hat, baiHatArrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ten_bai_hat = listView.getItemAtPosition(position).toString();
startActivity(new Intent(getApplicationContext(),SecondActivity.class)
.putExtra("BAIHAT", baiHatArrayList)
.putExtra("TENBAIHAT", ten_bai_hat)
.putExtra("POSITION",position));
}
});
}
private void Addlist(){
listView = (ListView) findViewById(R.id.listViewdanhsach);
baiHatArrayList = new ArrayList<>();
baiHatArrayList.add(new BaiHat("Bến tương tư - Quang Linh", R.raw.ben_tuong_tu_quang_linh));
baiHatArrayList.add(new BaiHat("Buồn trong kỷ niệm - Đan Nguyên", R.raw.buon_trong_ky_niem_dan_nguyen));
baiHatArrayList.add(new BaiHat("Cát bụi cuộc đời - Phi Nhung", R.raw.cat_bui_cuoc_doi_phi_nhung));
baiHatArrayList.add(new BaiHat("Cho vừa lòng em - Như Nguyệt", R.raw.cho_vua_long_em_nhu_nguyet));
baiHatArrayList.add(new BaiHat("Chuyến đò không em - Tuấn Vũ", R.raw.chuyen_do_khong_em_tuan_vu));
}
}
package com.myappnvt.truyendulieu;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;
public class SecondActivity extends AppCompatActivity {
String bai_hat ="";
int vi_tri =0;
TextView txtlaydulieu;
ArrayList<BaiHat> baiHatArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
txtlaydulieu = (TextView) findViewById(R.id.textViewlaydulieu);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
baiHatArrayList = (ArrayList) bundle.getParcelableArrayList("BAIHAT");
bai_hat = baiHatArrayList.get(vi_tri).getTenBaiHat().toString();
String songName = intent.getStringExtra("TENBAIHAT");
txtlaydulieu.setText(songName);
txtlaydulieu.setSelected(true);
vi_tri = bundle.getInt("POSITION",0);
Uri uri = Uri.parse(baiHatArrayList.get(vi_tri).toString());
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
mediaPlayer.start();
}
}