Java 类型new Thread(){}的方法getSystemService(String)未定义
我正在尝试对我的代码进行多线程处理,除了在标题中读取的错误外,没有其他错误。我对编码还很陌生,所以现在我不得不把我的代码翻过来让应用程序平稳运行,我不确定如何解决这个问题 我的Java代码:Java 类型new Thread(){}的方法getSystemService(String)未定义,java,android,multithreading,Java,Android,Multithreading,我正在尝试对我的代码进行多线程处理,除了在标题中读取的错误外,没有其他错误。我对编码还很陌生,所以现在我不得不把我的代码翻过来让应用程序平稳运行,我不确定如何解决这个问题 我的Java代码: package com.bipbapapps.leagueclickerapp; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import
package com.bipbapapps.leagueclickerapp;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainClass extends Activity implements OnClickListener {
public float goldCount;
Button minionClick;
Button storeClick;
Button storeDismiss;
TextView textGoldCount;
ImageView spinningBars;
String textTotal;
private SharedPreferences prefs;
PopupWindow pw;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full-screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mainlayout);
// Initialize variables from popup window
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.storemenu,
null, false));
storeDismiss = (Button) pw.getContentView().findViewById(
R.id.menudismissid);
prefs = getSharedPreferences("LeagueClicker", Context.MODE_PRIVATE);
goldCount = prefs.getFloat("goldCount", 0.0f);
// Linking the variables
minionClick = (Button) findViewById(R.id.minioncentreid);
storeClick = (Button) findViewById(R.id.storeimageid);
textGoldCount = (TextView) findViewById(R.id.textviewtop);
spinningBars = (ImageView) findViewById(R.id.spinningbarsid);
// String which will display at the top of the app
textTotal = goldCount + " Gold";
// Setting TextView to the String
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
Typeface tf = Typeface.createFromAsset(getAssets(), "mechanical.ttf");
textGoldCount.setTypeface(tf);
textGoldCount.setTextSize(35);
// Setting onClickListeners
minionClick.setOnClickListener(this);
storeClick.setOnClickListener(this);
storeDismiss.setOnClickListener(this);
}
@Override
public void onPause() {
super.onPause();
prefs.edit().putFloat("goldCount", goldCount).commit();
}
@Override
public void onResume() {
super.onResume();
goldCount = prefs.getFloat("goldCount", 0.0f);
t.start();
}
@Override
public void onStop() {
super.onStop();
prefs.edit().putFloat("goldCount", goldCount).commit();
Log.d(prefs.getFloat("goldCount", 0.0f) + "derprolw", "ejwfjbrea");
}
public void barsAnimation() {
Animation rotation = AnimationUtils.loadAnimation(this,
R.anim.spinningbarsanimation);
rotation.setRepeatCount(Animation.INFINITE);
spinningBars.startAnimation(rotation);
}
Thread t = new Thread(){
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.minioncentreid:
goldCount += 1.0;
prefs.edit().putFloat("goldCount", goldCount).commit();
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
break;
case R.id.storeimageid:
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupview = inflater.inflate(R.layout.storemenu, null);
final PopupWindow pw = new PopupWindow(popupview, 300, 450);
pw.showAtLocation(v, Gravity.CENTER, 0, 0);
storeDismiss = (Button) pw.getContentView().findViewById(
R.id.menudismissid);
storeDismiss.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pw.dismiss();
}
});
pw.showAsDropDown(storeClick, 0, 0);
break;
}
};
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
该线程位于线程t=new thread{。有人知道我可以做什么来解决这个问题吗?这是该线程的上下文,而您实际上正在寻找使用该活动的.getSystemService
改用:
MainClass.this.getSystemService您是指MainClass.this.Nice,它清除了这个错误:尽管现在Eclipse告诉我类型为new Thread{}的onClickView方法从不在本地使用。这意味着什么?您设置侦听器的部分,即设置onClickListeners是指主类中的空单击侦听器。您的意思是在线程内使用单击侦听器的内容吗?请将答案标记为正确。