我一直试图解决这个问题很长一段时间,我已经在Android工作室模拟器上尝试了它并且它提供了正确的输出,但是如果我在真实设备上尝试它它只在第一次尝试时给出正确的结果,然后textview只是不会更新,它总是显示第一个结果,我必须关闭应用程序并重新运行它以获得不同的结果。我将进程移到了onStart()但它仍然会出现同样的问题。
public class IdenBiner extends Activity {
@Override
protected void onStart() {
super.onStart();
setContentView(R.layout.activity_iden_biner);
Intent intx = getIntent();
Bitmap bmp = intx.getParcelableExtra(IdenPilih.PIX_KEY);
String psn = intx.getStringExtra(IdenPilih.PIX_NAME);
TextView t = (TextView) findViewById(R.id.txtIdenBiner);
t.setText(null);
for (int i = 0; i < bmp.getWidth(); i++) {
for (int j = 0; j < bmp.getHeight(); j++) {
int p = bmp.getPixel(i, j);
int r = Color.red(p);
int g = Color.green(p);
int b = Color.blue(p);
if(r < 127 && g < 127 && b < 127)
t.append("1");
else
t.append("0");
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iden_biner);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_iden_biner, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public int[] chrToint(char chr[]){
int result[] = new int[chr.length];
for(int i = 0; i < chr.length; i++) {
if (chr[i] == 49 )
result[i] = 1;
else
result[i] = 0;
}
return result;
}
public String arrToString(int arr[]){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < arr.length; i++)
sb.append(Integer.toString(arr[i]));
return sb.toString();
}
public String cariJenis(int id){
BufferedReader input = null;
File file = null;
String fileJenis = getResources().getString(R.string.namaJenis);
int count = -1;
try {
file = new File(Environment.getExternalStorageDirectory(), fileJenis);
input = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = input.readLine()) != null) {
count++;
if(count == id) return line;
}
} catch (IOException e) {
e.printStackTrace();
}
return "type not found";
}
public void bukaIdenProses(View view){
prosesEigen(view);
}
public void prosesEigen(View view){
BufferedReader input = null;
File file = null;
//Context context = getApplicationContext();
String fileName = getResources().getString(R.string.namaFile);
TextView t = (TextView) findViewById(R.id.txtIdenBiner);
int totFV[] = null;
int count = 0;
List<String> tmpFV = new ArrayList<String>();
try {
file = new File(Environment.getExternalStorageDirectory(), fileName);
input = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
StringBuilder buffer = new StringBuilder();
while ((line = input.readLine()) != null) {
//Log.d("FV", line); //-----------FVn-------------
count++;
buffer.append(line);
int fv[] = null;
fv = chrToint(line.toCharArray());
tmpFV.add(line);
totFV = Eigen.addVector(fv,totFV);
}
} catch (IOException e) {
e.printStackTrace();
}
//Log.d("totFV", arrToString(totFV)); //----------total FV--------------
int RFV[] = null;
RFV = Eigen.avgFlatVector(totFV, count);
//Log.d("RFV",arrToString(RFV)); //-----------RFV-------------
String sFV[] = tmpFV.toArray(new String[tmpFV.size()]);
int FV[][] = new int[count][];
for(int i = 0; i < count; i++)
FV[i] = chrToint(sFV[i].toCharArray());
int EF[][] = new int[count][];
for(int i = 0; i < count; i++) {
EF[i] = Eigen.subsVector(FV[i], RFV);
//Log.d("EF", arrToString(EF[i])); //-----------EFn-------------
}
Intent intx = getIntent();
Bitmap bmp = intx.getParcelableExtra(IdenPilih.PIX_KEY);
int FVG[] = new int[bmp.getWidth() * bmp.getHeight()];
int k = 0;
for (int i = 0; i < bmp.getWidth(); i++) {
for (int j = 0; j < bmp.getHeight(); j++) {
int p = bmp.getPixel(i, j);
int r = Color.red(p);
int g = Color.green(p);
int b = Color.blue(p);
if(r < 127 && g < 127 && b < 127)
FVG[k] = 1;
else
FVG[k] = 0;
k++;
}
}
//Log.d("FVG", arrToString(FVG)); //----------FVG--------------
int EFG[] = null;
EFG = Eigen.subsVector(FVG, RFV);
//Log.d("EFG", arrToString(EFG)); //----------EFG--------------
int ID[][] = new int[count][];
for(int i = 0; i < count; i++) {
ID[i] = Eigen.subsVector(EF[i], EFG);
//Log.d("ID", arrToString(ID[i])); //-----------IDn-------------
}
int JI[] = new int[count];
for(int i = 0; i < count; i++) {
JI[i] = Eigen.arrSum(ID[i]);
//Log.d("JI", Integer.toString(JI[i])); //-----------JI-------------
}
int minId = -1;
int tmpMin = Integer.MAX_VALUE;
for(int i = 0; i < count; i++){
if(JI[i] < tmpMin){
tmpMin = JI[i];
minId = i;
}
}
//Log.d("MIN", Integer.toString(minId)); //-----------minimum value------
t.setText(null);
t.setText("The picture pattern identified as " + cariJenis(minId));
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.alvin.identifikasimalaria.IdenBiner"
android:scrollIndicators="right">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/lblBtnIdenProses"
android:id="@+id/btnIdenProses"
android:onClick="bukaIdenProses"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtIdenBiner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</LinearLayout>
</ScrollView>
有人可以帮帮我吗。
答案 0 :(得分:0)
我发现代码没有问题。仿真器似乎具有较少的受限内存管理,每次重新创建活动时都会自动重新分配。因此,在模拟器上,为每个活动的状态分配的内存似乎被清除,但在真实设备上却没有,特别是在处理大数据意图时。