无法实例化Intent类型 [英] Cannot instantiate the type Intent

查看:60
本文介绍了无法实例化Intent类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:无法实例化意图类型"

Error: "Cannot instantiate the type Intent"

目标:根据gridView位置值启动新的意图

Objective: Launch a new intent depending on gridView position value

请求:针对上述错误提供帮助

Request: Assistance with the error specified above

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    ImageCell i = (ImageCell) v;
    trace("onItemClick in view: " + i.mCellNumber);
    Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT).show();

    switch (position) {
    case 0:
    // start one activity
        Intent intent = new Intent(DragActivity.this, Activity1.class);         
        startActivity(intent);
    break;
    case 1:
    // start another activity

    break;
    // etc.
    default:
        Toast.makeText(DragActivity.this, "Default", Toast.LENGTH_SHORT).show();
    }
}

我不确定我做错了什么(我尝试添加正确的导入等,但是我不确定可以做些什么来解决此问题),任何建议都将不胜感激.

I'm not sure exactly what I've done wrong (I've tried adding correct import etc but I'm not sure what else can be done to resolve this issue) Any suggestions are greatly appreciated.

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;


    public class DragActivity extends Activity implements
    View.OnLongClickListener, View.OnClickListener, View.OnTouchListener{

        /**
         */
        // Constants

        private static final int HIDE_TRASHCAN_MENU_ID = Menu.FIRST;
        private static final int SHOW_TRASHCAN_MENU_ID = Menu.FIRST + 1;
        private static final int ADD_OBJECT_MENU_ID = Menu.FIRST + 2;
        private static final int CHANGE_TOUCH_MODE_MENU_ID = Menu.FIRST + 3;
        private boolean isErase = true;
        private EditText et;
        private TextView tx;

        /**
         */
        // Variables

        private DragController mDragController; // Object that handles a drag-drop
        // sequence. It intersacts with
        // DragSource and DropTarget
        // objects.
        private DragLayer mDragLayer; // The ViewGroup within which an object can be
        // dragged.
        private DeleteZone mDeleteZone; // A drop target that is used to remove
        // objects from the screen.
        private int mImageCount = 0; // The number of images that have been added to
        // screen.
        private ImageCell mLastNewCell = null; // The last ImageCell added to the
        // screen when Add Image is clicked.
        private boolean mLongClickStartsDrag = true; // If true, it takes a long
        // click to start the drag
        // operation.
        // Otherwise, any touch
        // event starts a drag.

        public static final boolean Debugging = false; // Use this to see extra

        // toast messages.

        /**
         */
        // Methods

        /**
         * Add a new image so the user can move it around. It shows up in the
         * image_source_frame part of the screen.
         * 
         * @param resourceId
         *            int - the resource id of the image to be added
         */

        public void addNewImageToScreen(int resourceId) {
            if (mLastNewCell != null)
                mLastNewCell.setVisibility(View.GONE);

            FrameLayout imageHolder = (FrameLayout) findViewById(R.id.image_source_frame);
            if (imageHolder != null) {
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
                        Gravity.CENTER);
                ImageCell newView = new ImageCell(this);
                newView.setImageResource(resourceId);
                imageHolder.addView(newView, lp);
                newView.mEmpty = false;
                newView.mCellNumber = -1;
                mLastNewCell = newView;
                mImageCount++;

                // Have this activity listen to touch and click events for the view.
                newView.setOnClickListener(this);
                newView.setOnLongClickListener(this);
                newView.setOnTouchListener(this);

            }
        }

        /**
         * Add one of the images to the screen so the user has a new image to move
         * around. See addImageToScreen.
         * 
         */

        public void addNewImageToScreen() {
            int resourceId = R.drawable.sqwhite;
            addNewTextToScreen();
            int m = mImageCount % 3;

            if (m == 1)
                resourceId = R.drawable.sqdrk;

            else if (m == 2)
                resourceId = R.drawable.sqwhite;
            addNewImageToScreen(resourceId);

        }

        private void addNewTextToScreen() {
            // TODO Auto-generated method stub

            // et.setVisibility(View.VISIBLE);
            if (isErase) {
                tx.setText(et.getText().toString());
            } else {
                tx.setText("");
                et.setVisibility(View.GONE);
            }
            isErase = !isErase;
        }

        /**
         * Handle a click on a view.
         * 
         */

        public void onClick(View v) {
            if (mLongClickStartsDrag) {
                // Tell the user that it takes a long click to start dragging.
            //  toast("Press and hold to drag an image.");
              positionListener();
              onItemClick(null, v, mImageCount, mImageCount);

            }
        }

        private void positionListener() {
            // TODO Auto-generated method stub

    //      GridView gridView = (GridView) findViewById(R.id.image_grid_view);
    //      gridView.setOnItemClickListener(new OnItemClickListener() {
    //          public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    //              Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT).show();
    //          }
    //      });
        }

        /**
         * Handle a click of the Add Image button
         * 
         */

        public void onClickAddImage(View v) {
            addNewImageToScreen();
        }

    //  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    //        Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT).show();
    //    }

        /**
         * onCreate - called when the activity is first created.
         * 
         * Creates a drag controller and sets up three views so click and long click
         * on the views are sent to this activity. The onLongClick method starts a
         * drag sequence.
         * 
         */

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.demo);
            et = (EditText) findViewById(R.id.editText1);
            et.setVisibility(View.INVISIBLE);
            tx = (TextView) findViewById(R.id.textView1);

            GridView gridView = (GridView) findViewById(R.id.image_grid_view);

            if (gridView == null)
                toast("Unable to find GridView");
            else {
                gridView.setAdapter(new ImageCellAdapter(this));
                // gridView.setOnItemClickListener (this);
            }

            mDragController = new DragController(this);
            mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
            mDragLayer.setDragController(mDragController);
            mDragLayer.setGridView(gridView);

            mDragController.setDragListener(mDragLayer);
            // mDragController.addDropTarget (mDragLayer);

            mDeleteZone = (DeleteZone) findViewById(R.id.delete_zone_view);

            // Give the user a little guidance.
            Toast.makeText(getApplicationContext(),
                    getResources().getString(R.string.instructions),
                    Toast.LENGTH_LONG).show();
        }

        /**
         * Build a menu for the activity.
         * 
         */

        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);

            menu.add(0, HIDE_TRASHCAN_MENU_ID, 0, "Hide Trashcan").setShortcut('1',
                    'c');
            menu.add(0, SHOW_TRASHCAN_MENU_ID, 0, "Show Trashcan").setShortcut('2',
                    'c');
            menu.add(0, ADD_OBJECT_MENU_ID, 0, "Add View").setShortcut('9', 'z');
            menu.add(0, CHANGE_TOUCH_MODE_MENU_ID, 0, "Change Touch Mode");

            return true;
        }

        /**
         * Handle a click of an item in the grid of cells.
         * 
         */

        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            ImageCell i = (ImageCell) v;
            trace("onItemClick in view: " + i.mCellNumber);
            Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT).show();

            switch (position) {
            case 0:
            // start one activity
                Intent intent = new Intent(DragActivity.this, Activity1.class);         
                startActivity(intent);
            break;
            case 1:
            // start another activity

            break;
            // etc.
            default:
                // unknown type! based on the language,
                // there should probably be some error-handling
                // here, maybe an exception
                Toast.makeText(DragActivity.this, "Default", Toast.LENGTH_SHORT).show();
            }
        }

        /**
         * Handle a long click. If mLongClick only is true, this will be the only
         * way to start a drag operation.
         * 
         * @param v
         *            View
         * @return boolean - true indicates that the event was handled
         */

        public boolean onLongClick(View v) {
            if (mLongClickStartsDrag) {

                // trace ("onLongClick in view: " + v + " touchMode: " +
                // v.isInTouchMode ());

                // Make sure the drag was started by a long press as opposed to a
                // long click.
                // (Note: I got this from the Workspace object in the Android
                // Launcher code.
                // I think it is here to ensure that the device is still in touch
                // mode as we start the drag operation.)
                if (!v.isInTouchMode()) {
                    toast("isInTouchMode returned false. Try touching the view again.");
                    return false;
                }
                return startDrag(v);
            }

            // If we get here, return false to indicate that we have not taken care
            // of the event.
            return false;
        }

        /**
         * Perform an action in response to a menu item being clicked.
         * 
         */

        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case HIDE_TRASHCAN_MENU_ID:
                if (mDeleteZone != null)
                    mDeleteZone.setVisibility(View.INVISIBLE);
                return true;
            case SHOW_TRASHCAN_MENU_ID:
                if (mDeleteZone != null)
                    mDeleteZone.setVisibility(View.VISIBLE);
                return true;
            case ADD_OBJECT_MENU_ID:
                // Add a new object to the screen;
                addNewImageToScreen();
                return true;
            case CHANGE_TOUCH_MODE_MENU_ID:
                mLongClickStartsDrag = !mLongClickStartsDrag;
                String message = mLongClickStartsDrag ? "Changed touch mode. Drag now starts on long touch (click)."
                        : "Changed touch mode. Drag now starts on touch (click).";
                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG)
                        .show();
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        /**
         * This is the starting point for a drag operation if mLongClickStartsDrag
         * is false. It looks for the down event that gets generated when a user
         * touches the screen. Only that initiates the drag-drop sequence.
         * 
         */

        public boolean onTouch(View v, MotionEvent ev) {
            // If we are configured to start only on a long click, we are not going
            // to handle any events here.
            if (mLongClickStartsDrag)
                return false;

            boolean handledHere = false;

            final int action = ev.getAction();

            // In the situation where a long click is not needed to initiate a drag,
            // simply start on the down event.
            if (action == MotionEvent.ACTION_DOWN) {
                handledHere = startDrag(v);
            }

            return handledHere;
        }

        /**
         * Start dragging a view.
         * 
         */

        public boolean startDrag(View v) {
            DragSource dragSource = (DragSource) v;

            // We are starting a drag. Let the DragController handle it.
            mDragController.startDrag(v, dragSource, dragSource,
                    DragController.DRAG_ACTION_MOVE);

            return true;
        }

        /**
         * Show a string on the screen via Toast.
         * 
         * @param msg
         *            String
         * @return void
         */

        public void toast(String msg) {
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
        } // end toast

        /**
         * Send a message to the debug log. Also display it using Toast if Debugging
         * is true.
         */

        public void trace(String msg) {
            Log.d("DragActivity", msg);
            if (!Debugging)
                return;
            toast(msg);
        }

    } // end class

推荐答案

我的猜测是,它与您对泛型的使用有关.我想知道如果将其更改为

My guess would be that it has something to do with your use of generics. I would see if you're able to get it to work if you change it to

public class DragActivity extends Activity implements
    View.OnLongClickListener, View.OnClickListener, View.OnTouchListener{
//...
}

创建Intent后,还需要确保使用Intent.

You also need to make sure you use the Intent after you create it.

startActivity(intent);

Switch语句也需要默认选项,例如:

Switch statements also need default options, like so:

switch (pos) {
        case 1:  //do something
                 break;
        case 2:  //do something
                 break;
        default: break;
    }

这篇关于无法实例化Intent类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆