English 中文(简体)
如何从其他班级获得 var 。
原标题:how to get var from other classes

我相信我有一个简单的问题... 但作为初学者,我再也看不到了

在一个班里,我想从另一个班里拿些Vars

第1类:

package com.blabla;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class SMSReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    //get shared preferences

    GetSettings test1 = new RingSettings();
    String theSMStext = test1.getSMStext();
    Boolean theActivateSMS = test1.getActivateSMS();    
}
}

第2类:

package com.blabla;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class GetSettings {

/**
 * @param args
 */
private String SMStext;
private Boolean ActivateSMS;


public static void RingSettings(Context context) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

    String SMStext = sp.getString("SMStext", "0");
    Boolean ActivateSMS = sp.getBoolean("ActivateSMS", false);
}

public String getSMStext(){
    return SMStext;
}

public Boolean getActivateSMS(){
    return ActivateSMS;
}
}

Eclipse在类1中给我的“RingSettings 无法确定类型”\\gt;在 GetSettings 测试1 = 新的 RingSettings ();

我做错什么了!

最佳回答

你在这里肯定有些事情要解决

  • RingSettings as defined in GetSettings returns type void not a GetSettings object.
  • RingSettings is defined to take a context as an argument.
  • RingSettings is defined as a method of a GetSettings object and should be called with GetSettings.RingSettings(context)
  • assuming you are trying to create a GetSettings object... GetSettings should have a constructor

这些只是几个要开始的。我无意在此无礼,但你应该用一些简单的例子来更好地学习爪哇的基本知识。 也就是说,如果你解决上述问题并重编你的代码,那么应该有人能让你更接近你试图完成的任务。

查看 new 关键字上的这个信息:(要启动)

< a href=> http://docs.oracle.com/javase/tuative/java/javaOO/objectation.html" rel="nofollow" 新操作员通过分配新对象的内存并返回该内存的引用,即为该类。 新操作员还援引物体构造器。

祝你好运

问题回答

最简单的解决办法是实施帮助者类:

public class GlobalVars extends Application {

    private static String value2;


    public static String getValue() {
        return value2;
    }

    public static void setValue(String value) {
        value2 = value;
    }
}

在 A 类中将值设定为 globalVars. setValue ("某处");

在 B 类中, 数值以 < code> string your_ value = GlobalVars.getValue (); 的形式在 B类中以 < code> 键入您的_ value = GlobalVars.getValue (); 获得数值





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签