Android应用程序不会运行

时间:2012-10-14 17:20:06

标签: android android-layout android-emulator android-widget

请帮我理解为什么我的应用没有运行。 我想检查刷卡是否有效,但它甚至不会在模拟器上启动。 由于我不熟悉编程,因为没有编译器错误,我找不到什么问题。

活动代码:     包enn.ericsson.sitezoeker;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {

    ViewFlipper viewFlipper = GlobalVars.viewFlipper;
    Animation slideLeftIn = GlobalVars.slideLeftIn;
    Animation slideLeftOut = GlobalVars.slideLeftOut;
    Animation slideRightIn = GlobalVars.slideRightIn;
    Animation slideRightOut = GlobalVars.slideRightOut;
    GestureDetector gestureDetector = GlobalVars.gestureDetector;
    View.OnTouchListener gestureListener = GlobalVars.gestureListener;

    public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils
                .loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils
                .loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this,
                R.anim.slide_right_out);

        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
        gestureDetector = new GestureDetector(this,
                (OnGestureListener) gestureListener);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        else
            return false;
    }

}

GlobarVars.java:     包enn.ericsson.sitezoeker;

import android.app.Application;
import android.view.GestureDetector;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ViewFlipper;

public class GlobalVars extends Application {

    private static GlobalVars vars;

    public static GlobalVars getInstance() {
        return vars;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        vars = this;
    }

    public static GestureDetector gestureDetector;
    public static View.OnTouchListener gestureListener;
    public static Animation slideLeftIn;
    public static Animation slideLeftOut;
    public static Animation slideRightIn;
    public static Animation slideRightOut;
    public static ViewFlipper viewFlipper;

}

MyGestureDetector类:     包enn.ericsson.sitezoeker;

import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.widget.ViewFlipper;

class MyGestureDetector extends SimpleOnGestureListener {

    ViewFlipper viewFlipper = GlobalVars.viewFlipper;
    Animation slideLeftIn = GlobalVars.slideLeftIn;
    Animation slideLeftOut = GlobalVars.slideLeftOut;
    Animation slideRightIn = GlobalVars.slideRightIn;
    Animation slideRightOut = GlobalVars.slideRightOut;

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {

            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                viewFlipper.setInAnimation(slideLeftIn);
                viewFlipper.setOutAnimation(slideLeftOut);
                viewFlipper.showNext();
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                viewFlipper.setInAnimation(slideRightIn);
                viewFlipper.setOutAnimation(slideRightOut);
                viewFlipper.showPrevious();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}

清单:

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:name=".GlobalVars"
    package="enn.ericsson.sitezoeker"
    android:installLocation="auto"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="me.user_interface.DisplayMessageActivity"/> 
    </application>

</manifest>

布局:          

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/View1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Provider:"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <RadioButton
                        android:id="@+id/rbKPN"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="KPN" />

                    <RadioButton
                        android:id="@+id/rbTMobile"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="T-Mobile" />

                    <RadioButton
                        android:id="@+id/rbVodafone"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Vodafone" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Site number:"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <EditText
                    android:id="@+id/txtSitenr"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="number" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="10dp" >
                </TableRow>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="72dp"
                    android:layout_height="wrap_content"
                    android:text="Address:"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblAddress"
                    android:layout_width="231dp"
                    android:layout_height="71dp"
                    android:background="@drawable/draw"
                    android:inputType="none"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#ffffff" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="10dp" >
                </TableRow>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="72dp"
                    android:layout_height="wrap_content"
                    android:text="City:"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblCity"
                android:layout_width="231dp"
                android:layout_height="71dp"
                android:background="@drawable/draw"
                android:inputType="none"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#ffffff" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="false"
    android:id="@+id/View2" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView31"
                android:layout_width="72dp"
                android:layout_height="wrap_content"
                android:text="Site name:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/lblSiteName"
                android:layout_width="231dp"
                android:layout_height="71dp"
                android:background="@drawable/draw"
                android:inputType="none"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#ffffff" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="10dp" >
            </TableRow>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView41"
                android:layout_width="72dp"
                android:layout_height="wrap_content"
                android:text="Site type:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/lblSiteType"
                android:layout_width="231dp"
                android:layout_height="71dp"
                android:background="@drawable/draw"
                android:inputType="none"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#ffffff" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="10dp" >
            </TableRow>
        </LinearLayout>
    </LinearLayout>



</ScrollView>

</ViewFlipper>

RG, 埃里克

3 个答案:

答案 0 :(得分:1)

我只检查了几行代码,但我注意到您没有调用super.onCreate()(并且您的onCreate方法应该是public而不是protected):< / p>

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

无法调用super.onCreate()会抛出SuperNotCalledException。来自onCreate文档:

  

派生类必须调用超类的此方法的实现。如果他们不这样做,将抛出异常。

如果您仍需要帮助,则需要发布新的LogCat错误。

答案 1 :(得分:0)

在主要活动中,将"public abstract class mainActivity"更改为"public class mainActivity" 然后将"protected void onCreate"更改为"public void onCreate。您还应该在主要活动中的onCreate类中包含"super.onCreate(savedInstanceState);"。这应该可以正常工作。

答案 2 :(得分:0)

我设法让它发挥作用。 问题是GestureDetector的东西。 它甚至没有调用类,我已经删除了两个并使用了TouchListener方法。

它现在正在滑动,但只是一次,所以没有完全修复,但到了那里......: - )