У меня проблема с этим приложением. Я думаю, что это вопрос контекста, но я не могу определить проблему, а затем решить ее либо ... Я уже разместил вопрос с подобным, но человек в комментариях (единственный, кто комментировал, сказал мне повторите вопрос, добавив более подробную информацию ... Дело в том, что у меня действительно нет других деталей! Я мог бы называть эту функцию из основного, но из-за этой трудности я НЕВИДИМО, и предпочитаю обрабатывать ее в упорядоченном дивиденде в разных классах ..Исключение Nullpointer на медиаплеере в другом классе
package org.altervista.xsparter.www.app01;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.os.Bundle;
/**
* Created by Domenico on 03/08/2015.
*/
public class actionview extends Activity {
public boolean[] isplaying = {false,false,false,false,false,false,false,false};
public boolean[] ditofound = {false,false,false,false,false,false,false,false};
public int[] numerazioniastiera = new int[8];
public boolean generalplay = false;
boolean ditoscreen = false;
public int action;
int x;
int y;
public void getSound(String nota){
Main Main = new Main();
//Se la nota è docentrale
Resources res = this.getResources();
int soundId = res.getIdentifier(nota, "raw", this.getPackageName());
MediaPlayer mp1 = MediaPlayer.create(this,soundId);
}
}
вызывающий метода:
public class MyTouchListener implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
actionview actionview = new actionview();
onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN){
switch(v.getId()){
case R.id.nota1:
System.err.println("Nota1");
actionview.getSound("c3");
//do stuff for button 1
break;
case R.id.nota2:
System.err.println("Nota2");
//do stuff for button
break;
case R.id.nota3:
System.err.println("Nota3");
//do stuff for button 1
break;
case R.id.nota4:
System.err.println("Nota4");
//do stuff for button 1
break;
case R.id.nota5:
System.err.println("Nota5");
//do stuff for button 1
break;
case R.id.nota6:
System.err.println("Nota6");
//do stuff for button 1
break;
case R.id.nota7:
System.err.println("Nota7");
//do stuff for button 1
break;
case R.id.nota8:
System.err.println("Nota8");
//do stuff for button 1
break;
}}
return true;
}
}
Logcat:
08-08 20:48:16.533 5056-5056/org.altervista.xsparter.www.app01 E/InputEventReceiver﹕ Exception dispatching input event.
08-08 20:48:16.534 5056-5056/org.altervista.xsparter.www.app01 E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
08-08 20:48:16.572 5056-5056/org.altervista.xsparter.www.app01 E/MessageQueue-JNI﹕ java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at org.altervista.xsparter.www.app01.actionview.getSound(actionview.java:27)
at org.altervista.xsparter.www.app01.Main$MyTouchListener.onTouch(Main.java:97)
at android.view.View.dispatchTouchEvent(View.java:7701)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
at android.view.View.dispatchPointerEvent(View.java:7886)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3982)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3861)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3425)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3475)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3444)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3551)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3452)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3608)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3425)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3475)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3444)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3452)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3425)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5630)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5610)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5581)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5710)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at c
08-08 20:48:16.579 5056-5056/org.altervista.xsparter.www.app01 D/AndroidRuntime﹕ Shutting down VM
08-08 20:48:16.579 5056-5056/org.altervista.xsparter.www.app01 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41694ba8)
08-08 20:48:16.583 5056-5056/org.altervista.xsparter.www.app01 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.altervista.xsparter.www.app01, PID: 5056
java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at org.altervista.xsparter.www.app01.actionview.getSound(actionview.java:27)
at org.altervista.xsparter.www.app01.Main$MyTouchListener.onTouch(Main.java:97)
at android.view.View.dispatchTouchEvent(View.java:7701)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
at android.view.View.dispatchPointerEvent(View.java:7886)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3982)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3861)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3425)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3475)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3444)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3551)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3452)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3608)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3425)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3475)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3444)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3452)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3425)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5630)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5610)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5581)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5710)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android
Будет ли эта помощь: Http: //stackoverflow.com/questions/8238588/how-to- call-getresources-from-a-class-which-has-no-context –
использует отладчик в вашей среде IDE. – MWiesner