2011-06-25 132 views
1

我知道这个问题已经被问过一百万次了,因为我已经做了一些研究并且发现了很多线程。我试图在这些线索中使用答案,但我遇到了一些麻烦。使用全局变量

我期待设置一些我可以在所有活动中使用的变量。

我创建了一个GlobalVariables.java类如下所示(该值存在只是为了测试目的截至目前):

import android.app.Application; 

public class GlobalVariables extends Application { 

int holeAmount; 

public int getHoles(){ 
    return holeAmount; 
    } 
    public void setHoles(String s){ 
    holeAmount = 30; 
    } 

} 
在我的主要活动

这里的一切正在发生的事情,我有以下:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    GlobalVariables global = ((GlobalVariables)getApplicationContext()); 
    int totalHoles = global.getHoles(); 

,并在 “GlobalVariables全球= ...” 行,我收到以下错误:

Multiple markers at this line 
- GlobalVariables cannot be resolved 
to a type 
- GlobalVariables cannot be resolved 
to a type 

我试图按照这里的说明,但很明显我做了一些不正确的事情。 >How to declare global variables in Android?

任何帮助将不胜感激!

谢谢!



第二次尝试:

EasyPar.java(错误@ EasyParHelperActivity)

package com.movi.easypar; 

import java.text.DateFormat; 
import java.util.Date; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.PixelFormat; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.ScrollView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class EasyPar extends Activity implements OnClickListener { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

(initialize all my buttons and textviews) 
} 

public void someMethod() { 
    EasyParHelperActivity.helper.setHoles(30); 
    int holes = EasyParHelperActivity.helper.getHoles(); 
} 

(the rest of my button functions, etc.) 

EasyParHelperActivity(无错误)

package com.movi.easypar; 

import android.app.Application; 

public class EasyParHelperActivity extends Application { 

public static EasyParHelper helper = new EasyParHelper(); 

} 

EasyParHelper.java(无错误)

package com.movi.easypar; 

public class EasyParHelper { 

private int holeAmount = 0; 

public int getHoles() { 
    return holeAmount; 
} 

public void setHoles(int holes) { 
    holeAmount = holes; 
} 
} 

我想要的是用户能够点击第一个屏幕上的按钮,“18”或“9”的,而对于应用程序能够在其他任何时间使用该值。所以我需要在屏幕1中设置它,在屏幕2中我需要检索该值。

+0

你可以发布应用程序的整个代码或最小的应用程序的错误?我有强烈的感觉,这是一个非常简单的错误(例如导入一个名为GlobalVariables的不同类)或更简单的东西。 – Augusto

+0

完成(名字已经改变了一点..但实质上,一切都应该是相同的。) – Rob

+0

我真的很抱歉Rob,我经历了4次代码,我不能发现问题:( – Augusto

回答

2

创建一个 '助手' 类,如下...

package com.my.application 

public class MyAppHelper { 

    private int holeAmount = 0; 

    public int getHoles() { 
     return holeAmount; 
    } 

    public void setHoles(int holes) { 
     holeAmount = holes; 
    } 
} 

然后为你的应用程序类执行下列操作...

package com.my.application 

public class MyApplication extends Application { 

    public static MyAppHelper helper = new MyAppHelper(); 

} 

要访问的get/set方法你可以简单地打电话给你的帮手...

package com.my.application 

public class MyActivity extends Activity { 

    // Normal onCreate(...) etc here 

    public void someMethod() { 
     MyApplication.helper.setHoles(30); 
     int holes = MyApplication.helper.getHoles(); 
    } 
} 
+0

我试过这个,但我越来越 - GlobalVariables不能解析为类型(我把GlobalVariables.getHoles();在我的主类,这是为什么?)我想从GlobalValues.java获取值MainApplication.java – Rob

+0

我的主类扩展了Activity,这会是一个问题吗?当我创建一个新类(右键单击我的包> new> class)并创建MyAppHelper,并尝试将其更改为protected时,它会抛出一个错误,指出类EasyParHelper的非法修饰符;只允许公开,抽象和最终被允许 – Rob

+0

@Rob:我的坏 - 助手类应该公开不受保护,我会编辑。不,不要紧,你的主类扩展活动。只要确保你的扩展应用程序和你的Activity都在同一个包中,你甚至不需要导入Application类。我经常使用'助手',这就是我的做法。 – Squonk

0

您需要导入GlobalVariables。你也需要把GlobalVariables的全类名应用程序的名称属性,清单如下所述: http://developer.android.com/guide/topics/manifest/application-element.html#nm

+0

我已经将GlobalVariables添加到清单,所以应该是好的,要导入它,我应该能够:“导入GlobalVariables;” – Rob

+0

它是否在同一个包中?我建议关闭并在Eclipse中打开项目(或刷新)。有时候一些编译错误可能来自错误的eclipse刷新。 –

+0

当我尝试将它添加到导入列表中时,即使我添加import com.my.application.GlobalVariables;它说导入com.my.applicaiton.GlobalVariables无法解析。这是因为它的扩展应用程序而不是偶然的活动? – Rob

0

如果你

GlobalVariables global = ((GlobalVariables)getApplication()); 

从文档是什么并不清楚你从getApplicationContext()得到实际应用对象或其他一些实现。从代码来看,它绝对不会出现。

为了您的目的,但是您希望得到您的Application对象,该对象将是您的GlobalVariables。并确保它在清单中正确注册。


也只是为了确保,你有

package com.my.application; 

截至GlobalVariables开始,不是吗?不要在你的代码片段中看到它。第一

+0

我有包com.my.application;在开始时,GlobalVariables.java文件没有任何错误。你试过以上,仍然提到什么没有骰子:(我只是得到多个标记在该行 \t - GlobalVariables解决不了 \t的类型 \t - GlobalVariables解决不了 \t的类型到处 – Rob

0

第一件事......

你应该深入考虑是否以及为什么你可能需要一个全局变量。全局变量只能用作最后的手段。此外,如果最终需要使用全局变量,则需要将它们记录为死亡,因为没有错误,如果您的应用程序变得复杂,您将失去对其中一个或多个的跟踪。跟踪他们是一个严重的PIA!

根据我的经验,几乎99%的时间使用全局变量,这是因为程序员的懒惰,他们试图实现的任何事情都可能以一种使用编程最佳实践的方式完成。

下面是一个例子: https://groups.google.com/forum/#!topic/android-developers/yfI89IElhs8

不知道更多关于你正在尝试你的类之间传递了什么,我不能提供比已经提供了我之外的任何其他建议。不过,我相当乐观,你可以在不使用任何全局变量的情况下实现你正在寻找的东西。

+0

我取消如果出现问题,请说明你对重大混乱的看法。然而,我试图避免的是,如果我改变了一件事情,就不得不多次编辑每个变量。相反,我宁愿将它改为1个地方,让它改变使用它的整个应用程序。对于可能导致问题的值,我可能不会使用全局变量。 – Rob

+1

然后,我会definitley建议使用字符串资源文件。例如,我使用SharedPreferences,并为键/值对中的键使用字符串资源,因为它是全局可用的。因此它看起来像this.mSharPref.getInt(this.getString(R.string.somevalue),0); – rf43

+0

@Rob btw虽然所有其他的答案是可行的,我想如果你去使用这个字符串prefs的路线,你会发现它是** WAY **更简单,** WAY **更容易维护。它还具有让Android能够完成工作的好处,您也可以做到这一点。对象创建/释放。 – rf43

0

我已经设置了,而且一直在为我工作是创建一个publ ic类,然后用任何类所称的上下文初始化它。我发现这在this链接。这实际上让我能够访问系统服务等内容。然后,我继续并定义了一些让我可以进出课程的类变量。但为了实际处理变量的存储,我使用了SharedPrefrences。 这个设置的好处在于,在访问每个变量之前,我不必为共享的prefrences设置代码。我只是通过公开课。 现在请记住,我是这个东西的完全新手,我不确定这是最好的方法,或者即使这样做应该以这种方式完成。但我决定分享我一直在做的一切。所以这里有一些代码:

package com.example.someapp; 

    import android.app.PendingIntent; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 

    public class GlobalThing { 

Context me; 
SharedPreferences appdata; 

public GlobalThing(Context me) { 
    this.me = me; 
    appdata = me.getApplicationContext().getSharedPreferences(
      "ApllicationData", 0); 
} 

public boolean alarmRunning() { 
    boolean b; 
    Intent i = new Intent(me, AlarmThing.class); 
    b = (PendingIntent.getBroadcast(me, 0, i, PendingIntent.FLAG_NO_CREATE) != null); 
    if (b) { 
     return true; 
    } else { 
     return false; 
    } 
} 

public void timeIn(long time){ 
    SharedPreferences.Editor timeVar = appdata.edit(); 
    timeVar.putLong("time delay", time); 
    timeVar.commit(); 
} 
public long timeOut(){ 
    return appdata.getLong("time delay", 0); 
} 
    } 

以上是实际的全球分类。这不会在清单或任何东西中声明。

然后访问的变量在这个类我会在活动代码是这样的:

 public class SmokeInterface extends Activity implements OnClickListener { 

GlobalThing gt; 
boolean bool; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_smoke_interface); 
      gt = new GlobalThing(this); 
       setalarm(); 
     } 

而且访问的变量是这样的:

 private void setAlarm() { 
    bool = gt.alarmRunning(); 
    if (bool) { 
     Toast.makeText(this, "Alarm is already running.", 
       Toast.LENGTH_SHORT).show(); 
    } else { 
     AlarmManager m = (AlarmManager) getSystemService(ALARM_SERVICE); 
     Intent i = new Intent(this, AlarmThing.class); 
     PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 
       PendingIntent.FLAG_CANCEL_CURRENT); 
     Calendar time = Calendar.getInstance(); 
     time.setTimeInMillis(System.currentTimeMillis()); 
     time.add(Calendar.SECOND, 10); 
     m.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pi); 
    } 
} 

或本...

gt.timeIn(60);//write time in (long) to shared prefrences 
    long l = gt.timeOut();//read time out (long) from shared prefrences