1

我在这里按照教程进行操作:http: //developer.samsung.com/android/technical-docs/Gestures-in-Android 如果我在一个单独的项目中执行它,它可以工作,但是,当我尝试在另一个项目中实现它时,像这样:

public class Main extends Activity implements OnGesturePerformedListener {
    int mDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int mMonth = Calendar.getInstance().get(Calendar.MONTH);
    int mYear = Calendar.getInstance().get(Calendar.YEAR);
    boolean listHasItems = false;
    public static String date = "";
    ArrayList<String> listNames = new ArrayList<String>();
    List<ActivityToDo> acts = System.activities;
    GestureLibrary gLibrary;
    GestureOverlayView mView;

      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        removeActivitesBeforeCurrentDate();
        readDB();

        gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (gLibrary != null) {
            if (!gLibrary.load()) {
                Log.e("GestureSample", "Gesture library was not loaded…");
                finish();
            } else {
                mView = (GestureOverlayView) findViewById(R.id.gestures);
                mView.addOnGesturePerformedListener(this);
            }
        }

        setContentView(R.layout.main);
        final Button btnNewAct = (Button) findViewById(R.id.btnNewAct);

        final DatePicker datep = (DatePicker) findViewById(R.id.datePicker);
        datep.init(mYear, mMonth, mDay, new OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year,
                    int monthOfYear, int dayOfMonth) {
                mDay = datep.getDayOfMonth();
                mMonth = datep.getMonth();
                mYear = datep.getYear();
                buildList(mDay, mMonth + 1, mYear);
            }
        });

        btnNewAct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent();
                i.setClass(getApplicationContext(), NewActivity.class);
                startActivity(i);
            }
        });

        final ListView listview = (ListView) findViewById(R.id.listview);

        // listNames.add("Pick date to search activity.");
        buildList(mDay, mMonth + 1, mYear);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listNames);
        listview.setAdapter(adapter);
        registerForContextMenu(listview);

    }

    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> predictions = gLibrary.recognize(gesture);
        // one prediction needed
        if (predictions.size() > 0) {
            Prediction prediction = predictions.get(0);
            // checking prediction
            if (prediction.score > 1.0) {
                // and action
                Toast.makeText(this, prediction.name,Toast.LENGTH_SHORT).show();
            }
        }       
    }

mView.addOnGesturePerformedListener(this); 返回一个空指针异常。即使我尝试通过 (Main.this),这也是类似问题Android: Nullpointer Exception in addOnGesturePerformedListener的解决方案的解决方案。

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".Main" >

<android.gesture.GestureOverlayView
    android:id="@+id/gestures"
    android:layout_width="50dp"
    android:layout_height="0dip"
    android:layout_weight="0.5" />

<ListView
    android:id="@+id/listview"
    android:layout_width="250dp"
    android:layout_height="200dp"
    android:layout_weight="0.5"
    android:background="@drawable/spinnerst" >
</ListView>
</LinearLayout>

你认为是什么问题?提前致谢。

编辑:如果我将侦听器传递给 mView.addOnGesturePerformedListener(this),它将不起作用:

OnGesturePerformedListener listener=new OnGesturePerformedListener() {
        @Override
        public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
            ArrayList<Prediction> predictions = gLibrary.recognize(gesture);
            // one prediction needed
            if (predictions.size() > 0) {
                Prediction prediction = predictions.get(0);
                // checking prediction
                if (prediction.score > 1.0) {
                    // and action
                    mostrarToast(prediction.name);
                }
            }   
        }
    };
    gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (gLibrary != null) {
        if (!gLibrary.load()) {
            Log.e("GestureSample", "Gesture library was not loaded…");
            finish();
        } else {
            mView = (GestureOverlayView) findViewById(R.id.gestures);
            mView.addOnGesturePerformedListener(listener);
        }
    }
4

1 回答 1

0

仅当您具有为该活动定义布局的 setContentView() 时,您才能在活动中使用 findViewById 方法。因此,在您的特定情况下,您不会调用 setContentView

(GestureOverlayView) findViewById(R.id.gestures)

在 mView 上调用任何方法时将返回 null 并导致 NullpointerException(在您的情况下为 mView.addOnGesturePerformedListener(this);)

于 2013-10-11T11:52:55.090 回答