我正在尝试在textView中显示toast消息,但它需要每秒更新一次以显示所有包名称(每秒)但我的代码中出现错误&#34 ;不能引用在不同方法中定义的内部类中的非最终变量pnum"
行中的" msgto.setText("优化" + procInfos.get(PNUM).processName);"
// display all package names
ActivityManager actvityManager = (ActivityManager) getApplicationContext()
.getSystemService(getApplicationContext().ACTIVITY_SERVICE);
List<RunningAppProcessInfo> procInfos = actvityManager
.getRunningAppProcesses();
for (int pnum = 0; pnum < procInfos.size(); pnum++) {
if ((procInfos.get(pnum)).processName.contains("android")
|| (procInfos.get(pnum)).processName.contains("system")
|| (procInfos.get(pnum)).processName.contains("huawei")
|| (procInfos.get(pnum)).processName.contains("adil")) {
} else {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
//do your thing here
TextView msgto = (TextView) findViewById(R.id.optimizedapp);
msgto.setText("optimized " +
procInfos.get(pnum).processName);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
}
我尝试将整个代码放在我的线程中,但在log cat中获取此错误(2秒后即线程时间)
06-30 06:35:15.379: E/AndroidRuntime(5281): FATAL EXCEPTION: Thread-12964
06-30 06:35:15.379: E/AndroidRuntime(5281): Process: com.example.xtremenetbooster, PID: 5281
06-30 06:35:15.379: E/AndroidRuntime(5281): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
// THREAD
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
// do your thing here
// display all package names
ActivityManager actvityManager = (ActivityManager) getApplicationContext()
.getSystemService(
getApplicationContext().ACTIVITY_SERVICE);
List<RunningAppProcessInfo> procInfos = actvityManager
.getRunningAppProcesses();
for (int pnum = 0; pnum < procInfos.size(); pnum++) {
if ((procInfos.get(pnum)).processName
.contains("android")
|| (procInfos.get(pnum)).processName
.contains("system")
|| (procInfos.get(pnum)).processName
.contains("huawei")
|| (procInfos.get(pnum)).processName
.contains("adil")) {
// Toast.makeText(getApplicationContext(),
// "system apps",
// Toast.LENGTH_SHORT).show();
} else {
TextView msgto = (TextView) findViewById(R.id.optimizedapp);
msgto.setText("optimized "
+ procInfos.get(pnum).processName);
}
}
//
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
} // THREAD
答案 0 :(得分:0)
当您在pnum
循环中明确定义pnum
msgto.setText("optimized " + procInfos.get(pnum).processName);
@ for
答案 1 :(得分:0)
我只想在循环中创建一个新的final int:
Dim result = JsonConvert.DeserializeObject(jsonstring) 'deserialize it
Dim tempfo As String = result("rgInventory").ToString 'get rgInventory
Console.WriteLine(tempfo)