gpt4 book ai didi

java - 从android中的另一个类调用mainactivity中的方法

转载 作者:行者123 更新时间:2023-12-02 13:23:00 24 4
gpt4 key购买 nike

我有一个带有 TextView 和三个图像按钮的 ListView 。当单击其中一个图像按钮时,我试图从主页调用一种方法。但我收到了这个错误:

  04-19 13:02:48.021 30996-30996/com.example.lama.audiotest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lama.audiotest, PID: 30996
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:92)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.media.MediaPlayer.create(MediaPlayer.java:1007)
at android.media.MediaPlayer.create(MediaPlayer.java:990)
at com.example.lama.audiotest.Main3Activity.play(Main3Activity.java:35)
at com.example.lama.audiotest.CustomAdapter$1.onClick(CustomAdapter.java:83)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我尝试移动这一行:

mp= MediaPlayer.create(Main3Activity.this,R.raw.alfatiha);

到 onCreate();然后它起作用了,但我想根据我通过播放方法获得的字符串来播放不同的音频。怎么解决这个问题?

我的主要 Activity :

public class Main3Activity extends Activity {

private static final String TAG = Main3Activity.class.getSimpleName();
static MediaPlayer mp;

/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);

ListView listView = (ListView) findViewById(R.id.list_data);

CustomAdapter customAdapter = new CustomAdapter();

listView.setAdapter(customAdapter);

}

public void play(String name){
if (name.equals("الفاتحه")){
mp= MediaPlayer.create(Main3Activity.this,R.raw.alfatiha);
mp.start();
}
}

public void pause(){
mp.pause();
}

public void stop(){
mp.stop();
}
}

我的列表适配器:

public class CustomAdapter extends BaseAdapter {
Main3Activity main= new Main3Activity();
private static final String TAG = CustomAdapter.class.getSimpleName();
ArrayList<DataModel> listArray;

public CustomAdapter() {
listArray = new ArrayList<DataModel>(5);
listArray.add(new DataModel("الفاتحه", 5, 1.8, "Java"));
listArray.add(new DataModel("البقرة", 10, 2.8, "Python"));
listArray.add(new DataModel("ال عمران", 15, 3.8, "Django"));

}

@Override
public int getCount() {
return listArray.size(); // total number of elements in the list
}

@Override
public Object getItem(int i) {
return listArray.get(i); // single item in the list
}

@Override
public long getItemId(int i) {
return i; // index number
}

@Override
public View getView(int index, View view, final ViewGroup parent) {

if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.list, parent, false);
}

final MediaPlayer mp;


final DataModel dataModel = listArray.get(index);

TextView textView = (TextView) view.findViewById(R.id.Itemname);
textView.setText(dataModel.getName());

ImageButton play = (ImageButton) view.findViewById(R.id.btnplay);
ImageButton pause = (ImageButton) view.findViewById(R.id.btnpause);
ImageButton stop = (ImageButton) view.findViewById(R.id.btnstop);

play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


main.play(dataModel.getName());
// Toast.makeText(parent.getContext(), "you clicked play for: " + dataModel.getName(), Toast.LENGTH_SHORT).show();
}
});

pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

main.pause();

// Toast.makeText(parent.getContext(), "you clicked pause for: " + dataModel.getName(), Toast.LENGTH_SHORT).show();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

main.stop();
// Toast.makeText(parent.getContext(), "you clicked stop for: " + dataModel.getName(), Toast.LENGTH_SHORT).show();
}
});



view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "string: " + dataModel.getName());
Log.d(TAG, "int: " + dataModel.getAnInt());
Log.d(TAG, "double: " + dataModel.getaDouble());
Log.d(TAG, "otherData: " + dataModel.getOtherData());

Toast.makeText(parent.getContext(), "view clicked: " + dataModel.getOtherData(), Toast.LENGTH_SHORT).show();
}
});

return view;
}
}

最佳答案

在您的 CustomAdapter 中,您将创建 Main3Activity 的新实例,而不是将当前的 Main3Activity 实例作为自定义适配器的参数传递。因此,当您尝试访问 Main3Activity 方法时,您会收到空指针异常。

要解决您的问题,请修改您的 CustomAdapter 构造函数以允许 Main3Activity 作为参数:

public class CustomAdapter extends BaseAdapter {
Main3Activity main;
private static final String TAG = CustomAdapter.class.getSimpleName();
ArrayList<DataModel> listArray;

public CustomAdapter(Main3Activity main) {
this.main = main;
listArray = new ArrayList<DataModel>(5);
listArray.add(new DataModel("الفاتحه", 5, 1.8, "Java"));
listArray.add(new DataModel("البقرة", 10, 2.8, "Python"));
listArray.add(new DataModel("ال عمران", 15, 3.8, "Django"));
}

然后,回到 Main3Activity.class,将 Activity 实例传递给 CustomAdapter:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
ListView listView = (ListView) findViewById(R.id.list_data);
CustomAdapter customAdapter = new CustomAdapter(this);
listView.setAdapter(customAdapter);
}

关于java - 从android中的另一个类调用mainactivity中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43492959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com