我有一個帶有文本視圖和三個圖像按鈕的列表視圖。我試圖在單擊其中一個圖像按鈕時從主頁面調用方法。但我得到這個錯誤:從android中的另一個類的主要活動調用方法
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();然後它工作,但我想播放不同的音頻,具體取決於我用play方法得到的字符串。如何解決這個問題?
我mainactivity:
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;
}
}
工作!非常感謝 :) –