为什么我的应用程序在启动后立即停止工作(但没有任何错误消息)?

时间:2015-01-24 15:34:44

标签: java android android-studio

我刚开始使用Android Studio进行一些简单的移动开发 该应用程序非常简单。在敲击或震动时它应该给出正面或负面反馈(某种随机决策支持) 不幸的是,它在开始后就停止了工作(“不幸的是”Soll ich?“停了下来。”)并且我不知道为什么。

我尝试在某些行中设置断点,但它们从未受到调试器的影响。我甚至找不到错误信息。

任何可能导致此类行为的想法?

以下是我的文件:

activity_main.xml中

<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"
    tools:context=".MainActivity"
    android:clickable="true"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@color/background_material_dark"
    android:onClick="onLayoutClick">

    <TextSwitcher
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textSwitcher" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/should_i_question"
            android:id="@+id/textView_main"
            android:layout_gravity="center_horizontal"
            android:textSize="60sp"
            android:textColor="@color/primary_text_default_material_dark"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView" />

    </TextSwitcher>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/hint0"
        android:id="@+id/textView_sub"
        android:layout_gravity="center"
        android:textColor="@color/hint_foreground_material_dark" />

</LinearLayout>

MainActivity.java

package de.hermannoffen.sollich;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends ActionBarActivity implements SensorEventListener {

    private static final int SHAKE_THRESHOLD = 600;

    private final String[] gAnswers;
    private long gLastUpdate = 0;
    private Random gRandom;
    private float gLast_x, gLast_y, gLast_z;
    private TextSwitcher gTextSwitcher;
    private SensorManager gSensorManager;
    private Sensor gAccelerometer;

    public MainActivity() {
        gAnswers = getResources().getStringArray(R.array.answers);
        gRandom = new Random();
    }

    private void calcAnswer() {
        gTextSwitcher.setText(gAnswers[gRandom.nextInt(gAnswers.length)]);

        TextView textView = (TextView) findViewById(R.id.textView_sub);
        textView.setText(R.string.hintN);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gTextSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
        gTextSwitcher.setInAnimation(this, android.R.anim.fade_in);
        gTextSwitcher.setOutAnimation(this, android.R.anim.fade_out);

        gSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        gAccelerometer = gSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        gSensorManager.registerListener(this, gAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause() {
        super.onPause();
        gSensorManager.unregisterListener(this);
    }

    protected void onResume() {
        super.onResume();
        gSensorManager.registerListener(this, gAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
     public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            long curTime = System.currentTimeMillis();

            if ((curTime - gLastUpdate) > 100) {
                long diffTime = (curTime - gLastUpdate);
                gLastUpdate = curTime;

                float speed = Math.abs(x + y + z - gLast_x - gLast_y - gLast_z)/ diffTime * 10000;

                if (speed > SHAKE_THRESHOLD) {
                    calcAnswer();
                }

                gLast_x = x;
                gLast_y = y;
                gLast_z = z;
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public void onLayoutClick(View view) {
        calcAnswer();
    }

}

logcat的

01-24 14:55:07.381  24082-24092/de.hermannoffen.sollich I/art﹕ Debugger is active
01-24 14:55:07.570  24082-24082/de.hermannoffen.sollich I/System.out﹕ Debugger has connected
01-24 14:55:07.570  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:07.770  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:07.970  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:08.171  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:08.371  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:08.571  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:08.771  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:08.972  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:09.172  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:09.372  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:09.572  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:09.772  24082-24082/de.hermannoffen.sollich I/System.out﹕ waiting for debugger to settle...
01-24 14:55:09.973  24082-24082/de.hermannoffen.sollich I/System.out﹕ debugger has settled (1376)

1 个答案:

答案 0 :(得分:1)

删除此

public MainActivity() {
        gAnswers = getResources().getStringArray(R.array.answers);
        gRandom = new Random();
    }

将它们移至onCreate()

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gAnswers = getResources().getStringArray(R.array.answers);
        gRandom = new Random();

        gTextSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
        //.....
   }