我必须方法
A:
String teststring = new String ("blublub");
乙:
System.out.println(teststring);
我该怎么做才能让 B 看到 A 的那个对象?
我已经尝试过公共 || 最后的东西,但这不是我认为的正确方式。
谢谢你的帮助
两种方法的完整代码
public void onWindowFocusChanged(boolean hasFocus){
if (hasFocus){
final String teststring= new String ("blubblub");
}
}
public void a() {
System.out.println(teststring);
}
将 MediaPlayer 放在方法之外时出现错误日志。
04-06 05:20:25.140: E/AndroidRuntime(12120): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{~.MainActivity}: java.lang.NullPointerException
04-06 05:20:25.140: E/AndroidRuntime(12120): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1803)
04-06 05:20:25.140: E/AndroidRuntime(12120): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1919)
04-06 05:20:25.140: E/AndroidRuntime(12120): at android.app.ActivityThread.access$1500(ActivityThread.java:160)
04-06 05:20:25.140: E/AndroidRuntime(12120): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1008)
04-06 05:20:25.140: E/AndroidRuntime(12120): at android.os.Handler.dispatchMessage(Handler.java:130)
04-06 05:20:25.140: E/AndroidRuntime(12120): at android.os.Looper.loop(SourceFile:351)
04-06 05:20:25.140: E/AndroidRuntime(12120): at android.app.ActivityThread.main(ActivityThread.java:4070)
04-06 05:20:25.140: E/AndroidRuntime(12120): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 05:20:25.140: E/AndroidRuntime(12120): at java.lang.reflect.Method.invoke(Method.java:538)
04-06 05:20:25.140: E/AndroidRuntime(12120): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906)
04-06 05:20:25.140: E/AndroidRuntime(12120): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:664)
04-06 05:20:25.140: E/AndroidRuntime(12120): at dalvik.system.NativeStart.main(Native Method)
04-06 05:20:25.140: E/AndroidRuntime(12120): Caused by: java.lang.NullPointerException
04-06 05:20:25.140: E/AndroidRuntime(12120): at android.media.MediaPlayer.create(MediaPlayer.java:697)
04-06 05:20:25.140: E/AndroidRuntime(12120): at~.MainActivity.<init>(MainActivity.java:79)
第 79 行是:私有 MediaPlayer mediaPlayerW = MediaPlayer.create(getApplicationContext(), R.raw.soft);
围绕这条线 79 是:
button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(this); }
private MediaPlayer mediaPlayerW = MediaPlayer.create(getApplicationContext(), R.raw.soft);
public void onWindowFocusChanged(boolean hasFocus)
有没有可能,它可能与getBaseContext有关?