程序崩溃,不知道原因

时间:2014-04-28 18:11:31

标签: android

非常感谢你帮助我。

我遇到了一个我设法为Android设备编写的程序的问题。

程序本身从用户那里得到一个数字 - n(1-9),它必须找到符合这个条件的数字对:a = b * n(使用AsyncTask)并在屏幕上给出结果。更重要的是,它必须使用SharedPreferences记住最后的结果。

以下是Java代码:

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.SharedPreferences;

public class MainActivity extends Activity {

TextView edn; 
EditText ed;
TextView txt;
private SharedPreferences preferences;
public static final String MY_PREFERENCES = "myPreferences";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edn = (TextView) findViewById(R.id.textView4);  
    ed = (EditText) findViewById(R.id.editText1);  
    txt = (TextView) findViewById(R.id.textView5); 
    preferences = getSharedPreferences(MY_PREFERENCES, Activity.MODE_PRIVATE);
    fillUIFromPreferences();
}

private void fillUIFromPreferences() 
{
    int text1 = preferences.getInt("lastN", 0);
    String text2 = preferences.getString("results", "");

    if(text1 == 0)
    {
        edn.setText("");
    } else {
        String text1_str = String.valueOf(text1);
        edn.setText(text1_str);
    }

    txt.setText(text2);
}

public void Getab(View v) throws InterruptedException, ExecutionException
{
    String ed_text = ed.getText().toString().trim();
    Integer number;

    if(ed_text.isEmpty())
    {
        Toast.makeText(this, "You did not enter a number.", Toast.LENGTH_SHORT).show();
    } else {
        number = Integer.parseInt(ed.getText().toString());
        edn.setText(ed_text);
        SharedPreferences.Editor preferencesEditor = preferences.edit();
        preferencesEditor.putInt("lastN", number);
        preferencesEditor.commit();
        new SideTask().execute(number);
    }
}

private class SideTask extends AsyncTask<Integer, Void, ArrayList<Pair<Integer, Integer>>> 
{       
    @Override
    protected ArrayList<Pair<Integer, Integer>> doInBackground(Integer...integers)
    {
        ArrayList<Pair<Integer, Integer>> array = new ArrayList<Pair<Integer, Integer>>();

        int a = 10000, b = 10000;

        while(b < 100000)
        {
            if(a == integers[0] * b && a < 100000)
            {
                array.add(Pair.create(b, a));
            } else if (a >= 100000) {
                b++;
                a = b;
            } else if (a < 100000) {
                a++;
            }
        }
        return array;
    }

    @Override
    protected void onPostExecute(ArrayList<Pair<Integer, Integer>> array)
    {
        for(int i = 0; i < array.size(); i++)
        {
            Pair<Integer, Integer> a = array.get(i);
            txt.append(a.first + " " + a.second + "\n");
        }
        SharedPreferences.Editor preferencesEditor = preferences.edit();
        preferencesEditor.putString("results", txt.toString());
        preferencesEditor.commit();
    }
} 
}

xml文件

<RelativeLayout 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"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="26dp"
    android:layout_marginTop="21dp"
    android:text="N"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="number" >
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="49dp"
    android:text="Calculate" 
    android:onClick="Getab"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1"
    android:layout_marginTop="52dp"
    android:text="Last calculation was for N = "
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="35dp"
    android:text="Last results:"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView3"
    android:text="" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/textView2"
    android:layout_toRightOf="@+id/textView2" />

</RelativeLayout>

logcat的:

04-28 16:32:21.737: W/dalvikvm(1257): threadid=12: thread exiting with uncaught exception (group=0xb3ad6ba8)
04-28 16:32:21.857: E/AndroidRuntime(1257): FATAL EXCEPTION: AsyncTask #1
04-28 16:32:21.857: E/AndroidRuntime(1257): Process: com.example.math, PID: 1257
04-28 16:32:21.857: E/AndroidRuntime(1257): java.lang.RuntimeException: An error occured while executing doInBackground()
04-28 16:32:21.857: E/AndroidRuntime(1257):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-28 16:32:21.857: E/AndroidRuntime(1257):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-28 16:32:21.857: E/AndroidRuntime(1257):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-28 16:32:21.857: E/AndroidRuntime(1257):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-28 16:32:21.857: E/AndroidRuntime(1257):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-28 16:32:21.857: E/AndroidRuntime(1257):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-28 16:32:21.857: E/AndroidRuntime(1257):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-28 16:32:21.857: E/AndroidRuntime(1257):     at java.lang.Thread.run(Thread.java:841)
04-28 16:32:21.857: E/AndroidRuntime(1257): Caused by: java.lang.OutOfMemoryError: [memory exhausted]
04-28 16:32:21.857: E/AndroidRuntime(1257):     at dalvik.system.NativeStart.main(Native Method)

管理以捕获错误,不再在启动时崩溃。仍然得到OOM。我会感激任何帮助。

1 个答案:

答案 0 :(得分:1)

onCreate之后setContentView初始化视图,因为findViewById在当前视图层次结构中查找视图。您需要先将布局设置为Activity,然后初始化视图

 TextView edn;  
 EditText ed;  
 TextView txt; 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
  edn = (TextView) findViewById(R.id.textView4);  
  ed = (EditText) findViewById(R.id.editText1);  
  txt = (TextView) findViewById(R.id.textView5);