0

我必须方法

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有关?

4

2 回答 2

4

在这段代码中:

public void onWindowFocusChanged(boolean hasFocus){
    if (hasFocus){
        final String teststring= new String ("blubblub");
    }
}    

public void a() {
    System.out.println(teststring);
}

虽然 testString 被声明为“在类中”,但它实际上是在类的 onWindowFocusChanged 方法内声明的,并且通过这样做,它仅在该方法内可见。要使其在整个类中可见,请在类中声明它,而不是在方法或构造函数中:

public class MyClass {
   // variable below declared *in* the class
   // and is visible throughout the class
   private String testString = "";
   private String anotherVariable; // declared but not instantiated


public void onWindowFocusChanged(boolean hasFocus){
    if (hasFocus){
        // don't redeclare the variable here, and don't use new String(...)
        // final String teststring= new String ("blubblub"); 

        anotherVariable = "blubblub"; // instantiated here
    }
}


   public void someMethod() {
      // variable is now visible inside of all non-static methods
      System.out.println(testString);
   }

}

此外,您要避免使用String myString = new String("Foo");,因为这可能会导致不必要的对象创建效率低下。而是使用String myString = "foo";which 将重新使用字符串池中的字符串(如果可用)。

于 2013-04-06T03:09:02.447 回答
1

你有一个错字。

只需将语句B中括号中的位中的 'teststring' 更改为 'teststring'

于 2013-04-06T03:00:19.163 回答