Java使方法中的对象可见

时间:2013-04-06 02:57:37

标签: java object

我必须使用方法

A:

String teststring = new String ("blublub");

B:

System.out.println(teststring);

我该怎么办才能看到B的对象?

我已经尝试过公开||最后的东西,但这不是我认为的正确方法。

thx求助

两种方法的整个代码

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

2 个答案:

答案 0 :(得分:3)

在此代码中:

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";,它将重新使用字符串池中的字符串(如果有的话)。

答案 1 :(得分:1)

你有一个错字。

只需在声明 B

中的括号中将'testring'更改为'teststring'