线程中的android应用程序类
我需要在我的线程中使用我的应用程序类,它是从InterService开始的。 在我的IntentService中,我有以下代码:线程中的android应用程序类,android,multithreading,Android,Multithreading,我需要在我的线程中使用我的应用程序类,它是从InterService开始的。 在我的IntentService中,我有以下代码: protected void onHandleIntent(Intent intent) { final ResultReceiver receiver = intent.getParcelableExtra("receiver"); context = getBaseContext(); app = (AppLoader)getApp
protected void onHandleIntent(Intent intent) {
final ResultReceiver receiver = intent.getParcelableExtra("receiver");
context = getBaseContext();
app = (AppLoader)getApplicationContext();
ConnectionThread thread = new ConnectionThread(receiver, context, app.getNewApp());
这是我的帖子:
public ConnectionThread (ResultReceiver receiver, Context context, AppLoader app)
{
this.receiver = receiver;
this.context = context;
this.activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
this.app = app;
}
@Override
public void run() {
Log.d("ConnectionThread","Starting Server Connection");
try {
while(isThereActivityRunning()) {
if(app.isInternetOn())
{
Log.d("ConnectionThread", app.getText());
results = sendGetMessage();
b.putString("results", results);
receiver.send(2, b);
}
这是我的申请表:
public class AppLoader extends Application{
private AppLoader newApp;
public void onCreate()
{
super.onCreate();
}
public AppLoader getNewApp()
{
if(newApp == null)
newApp = new AppLoader();
return newApp;
}
我得到一个java.lang.NullPointerException,我不明白为什么..您不能创建自己的应用程序实例,即
newApp=newapploader()代码>
不该被叫来。Android为您创建应用程序,或者至少如果您在清单中声明了应用程序类,即
它将编译,但您将无法访问Android实例化应用程序通常会访问的任何内容
假设您拥有如上所述的清单,您已经可以通过调用以下命令访问应用程序实例:
app=(AppLoader)getApplicationContext()代码>
因此,请使用该选项并删除getNewApp()方法。如果没有stacktrace,我们将无法帮助您。异常通常会有一行代码告诉您异常发生的确切位置。如果在启动线程之前从未调用过getNewApp()
,我想,app
可能会为空,但根据您提供的内容,这很难判断。但是我在线程结构中使用了getNewApp。您是对的,我以前尝试过它,但出于某种原因,我没有这样做。。。我已经重写了,现在可以了,谢谢!