强制关闭按钮

时间:2012-01-08 19:27:33

标签: android button android-activity forceclose

当我点击按钮开始第一个活动

时,为什么我的应用程序强制关闭

这是启动活动:

public class ForeverAlone extends Activity implements OnClickListener{
    /** Called when the activity is first created. */

    Button start;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start = (Button) findViewById(R.id.bStart);
        start.setOnClickListener(this);
    }

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(ForeverAlone.this, First.class);
                ForeverAlone.this.startActivity(myIntent);
            }

    }

这是我按下按钮后出现的活动:

public class First extends Activity implements OnClickListener {

    Button firstNext;
    EditText firstFacebook, firstReallife;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        firstNext = (Button) findViewById(R.id.bFirstNext);
        firstFacebook = (EditText) findViewById(R.id.etFirst1);
        firstReallife = (EditText) findViewById(R.id.etFirst2);
        firstNext.setOnClickListener(this);
        String firstFB = firstFacebook.getText().toString();
        String firstRL = firstReallife.getText().toString();
        DataHelper.insert(firstFB, firstRL);
    }



        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(First.this, Second.class);
            First.this.startActivity(myIntent);
        }
}

任何帮助都将不胜感激!

[编辑]

这是LogCat:

01-08 19:19:55.656: D/dalvikvm(363): GC_EXTERNAL_ALLOC freed 50K, 53% free 2553K/5379K, external 1625K/2137K, paused 76ms
01-08 19:19:57.966: D/AndroidRuntime(363): Shutting down VM
01-08 19:19:57.966: W/dalvikvm(363): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-08 19:19:57.986: E/AndroidRuntime(363): FATAL EXCEPTION: main
01-08 19:19:57.986: E/AndroidRuntime(363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kenning.foreveralone/com.kenning.foreveralone.First}: java.lang.NumberFormatException: unable to parse '' as integer
01-08 19:19:57.986: E/AndroidRuntime(363):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-08 19:19:57.986: E/AndroidRuntime(363):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-08 19:19:57.986: E/AndroidRuntime(363):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-08 19:19:57.986: E/AndroidRuntime(363):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-08 19:19:57.986: E/AndroidRuntime(363):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 19:19:57.986: E/AndroidRuntime(363):  at android.os.Looper.loop(Looper.java:123)
01-08 19:19:57.986: E/AndroidRuntime(363):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-08 19:19:57.986: E/AndroidRuntime(363):  at java.lang.reflect.Method.invokeNative(Native Method)
01-08 19:19:57.986: E/AndroidRuntime(363):  at java.lang.reflect.Method.invoke(Method.java:507)
01-08 19:19:57.986: E/AndroidRuntime(363):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-08 19:19:57.986: E/AndroidRuntime(363):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-08 19:19:57.986: E/AndroidRuntime(363):  at dalvik.system.NativeStart.main(Native Method)
01-08 19:19:57.986: E/AndroidRuntime(363): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
01-08 19:19:57.986: E/AndroidRuntime(363):  at java.lang.Integer.parseInt(Integer.java:362)
01-08 19:19:57.986: E/AndroidRuntime(363):  at java.lang.Integer.parseInt(Integer.java:332)
01-08 19:19:57.986: E/AndroidRuntime(363):  at java.lang.Integer.<init>(Integer.java:105)
01-08 19:19:57.986: E/AndroidRuntime(363):  at com.kenning.foreveralone.DataHelper.insert(DataHelper.java:9)
01-08 19:19:57.986: E/AndroidRuntime(363):  at com.kenning.foreveralone.First.onCreate(First.java:27)
01-08 19:19:57.986: E/AndroidRuntime(363):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-08 19:19:57.986: E/AndroidRuntime(363):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-08 19:19:57.986: E/AndroidRuntime(363):  ... 11 more

[编辑]

我找到了“强行关闭”问题的原因。

这些代码行:

String firstFB = firstFacebook.getText().toString();
String firstRL = firstReallife.getText().toString();
DataHelper.insert(firstFB, firstRL);

如何阻止方法进入它们并以某种方式跳过它们?

3 个答案:

答案 0 :(得分:1)

如果您发布了logcat输出会有所帮助,但我的猜测是您没有将First列为清单中的活动。无论是First.onCreate中的某个内容还是正在生成异常。

答案 1 :(得分:0)

您有NumberFormatException。根据您的logcat输出,您将插入一个空字符串,其中Datahelper类中需要一个整数:

at com.kenning.foreveralone.DataHelper.insert(DataHelper.java:9)

答案 2 :(得分:0)

您正在获取EditTexts的实例

    firstFacebook = (EditText) findViewById(R.id.etFirst1);
    firstReallife = (EditText) findViewById(R.id.etFirst2);

然后获取他们的值

    String firstFB = firstFacebook.getText().toString();
    String firstRL = firstReallife.getText().toString();

并在这里使用

    DataHelper.insert(firstFB, firstRL);

但是EditTexts是空的,所以你实际上是在传入一个空字符串,因此

Caused by: java.lang.NumberFormatException: unable to parse '' as integer