我是新來的android,我嘗試開發一個系統,但是當我完成代碼時,處理程序顯示此警告這個處理程序類應該是靜態的或者可能發生泄漏(com.test.test3.ui.MainActivity.1)
顯示我編輯後的代碼,事件ontounch中的處理程序顯示警告處理程序無法解析。我試着把//忽略處理程序,我嘗試運行應用程序及其結果強制關閉。
public class MainActivity extends Activity {
protected static final int STOP = 100;
ImageView iv;
private ProgressBar pb;
LinearLayout ll;
private AnimationDrawable anim;
ScrollView sv;
private SQLiteDatabase db;
private boolean flagscanning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = new LinearLayout(this);
new HandlerClass(this);
db = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory()+"/antivirus.sqlite", null, SQLiteDatabase.OPEN_READONLY);
iv = (ImageView) this.findViewById(R.id.imageView1);
//掃描病毒進度條
pb = (ProgressBar) this.findViewById(R.id.progressBar1);
ll = (LinearLayout) this.findViewById(R.id.ll);
//設置ImageView背景資源爲動畫文件
iv.setBackgroundResource(R.drawable.bg);
//sv用來顯示病毒的掃描結果
sv = (ScrollView) this.findViewById(R.id.scrollView1);
anim = (AnimationDrawable) iv.getBackground();
}
private static class HandlerClass extends Handler{
private final WeakReference<MainActivity> mTarget;
public HandlerClass(MainActivity context){
mTarget = new WeakReference<MainActivity>((MainActivity) context);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
MainActivity target = mTarget.get();
if(msg.what==STOP){
target.ll.removeAllViews();
//anim.stop();
}
String str = (String) msg.obj;
TextView tv = new TextView(target);
tv.setText(str);
target.ll.setOrientation(LinearLayout.VERTICAL);
target.ll.addView(tv);
//sv.scrollBy(0, 20);
System.out.println(str);
}
};
@Override
public boolean onTouchEvent(MotionEvent event) {
//如果程序正在殺毒過程中,拒絕再次啓動殺毒線程
if(flagscanning){
return false;
}
//如果用戶觸摸屏幕,則開啓殺毒線程
if (event.getAction() == MotionEvent.ACTION_UP) {
flagscanning= true;
anim.start();
new Thread() {
public void run() {
// 獲取每一個應用程序的簽名,簽名須與數據庫的簽名想比較
List<PackageInfo> infos = getPackageManager()
.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_SIGNATURES);
//設置進度條的掃描範圍
pb.setMax(infos.size());
int total = 0;
int virustotal = 0;//設置初始病毒數爲0
for (PackageInfo info : infos) {
total++;
try {
sleep(20);//只爲便於觀察掃描效果和進度,無實質作用
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = Message.obtain();
msg.obj = "正在掃描" + info.packageName;
_handler.sendMessage(msg);_
Signature[] signs = info.signatures;
String str = signs[0].toCharsString();
String md5 = MD5Encoder.encode(str);
//將應用程序簽名與數據庫中保存的簽名進行比較,如果相一致,則使病毒數加1,並通過handler在界面顯示病毒包名
Cursor cursor = db.rawQuery("select desc from datable where md5=?",new String[] { md5 });
if (cursor.moveToFirst()) {
String desc = cursor.getString(0);
msg = Message.obtain();
msg.obj = info.packageName + ": " + desc;
_handler.sendMessage(msg);_
virustotal++;
}
cursor.close();
pb.setProgress(total);
}
Message msg = Message.obtain();
msg.what = STOP;
msg.obj = "掃描完畢 ,共發現" + virustotal + "個病毒";
_handler.sendMessage(msg);_
flagscanning = false;
pb.setProgress(0);
};
}.start();
}
return super.onTouchEvent(event);
}
@Override
protected void onDestroy() {
if (db.isOpen())
db.close();
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
讓你的處理程序類的靜態。 http://stackoverflow.com/questions/3106912/why-does-android-prefer-static-classes – Raghunandan
聲明'HanderClass hc'作爲類成員。然後在onCreate中用'hc = new HandlerClass(this);'替換'new HandlerClass(this);'。而不是'handler.sendMessage'使用'hc.sendMessage'。 – Raghunandan