开发者

How to stop email intent to cause force close error?

I just ran into a problem when trying to launch the phone's email app via a button in the options menu because as soon as I click the button in the emulator it causes a force close error. If s.o. help me out here to find the error in my code, that would be great.


This is my java file:

package com.mobilevideoeditor.moved;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class ShareGalleryView extends Activity {

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

        GridView vGrid=(GridView) findViewById(R.id.vgrid);
        registerForContextMenu(vGrid);
        vGrid.setAdapter(new VideoAdapter(this));


        vGrid.setOnItemClickListener(new OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // TODO Auto-generated method stub

                openOptionsMenu();



            }


        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu_gallery_share, menu);
      return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected (MenuItem item){
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("plain/text"); 
    if (item.getItemId() == R.id.menu_facebook)
        {
         //TODO open fb
            return true;

        }
        else if (item.getItemId() == R.id.menu_youtube)
        {
            //TODO open youtube
            return true;

        }
        else if (item.getItemId() == R.id.menu_email)
        {

          startActivity(emailIntent);  

            return true;

        }
        else if (item.getItemId() == R.id.menu_bluetooth)
        {
            // TODO send via bluetooth
            return true;

        }

        return super.onContextItemSelected(item);
    }

    public class VideoAdapter extends BaseAdapter {
        private Context mContext;

        public VideoAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_2,
                R.drawable.sample_6, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_1

        };

    }


}

This is what the DDMS prints out:

07-16 01:05:55.782: WARN/System.err(2096): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND }
07-16 01:05:55.792: WARN/System.err(2096):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
07-16 01:05:55.812: WARN/System.err(2096):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
07-16 01:05:55.822: WARN/System.err(2096):     at android.app.Activity.startActivityFromChild(Activity.java:2989)
07-16 01:05:55.822: WARN/System.err(2096):     at android.app.Activity.startActivityForResult(Activity.java:2769)
07-16 01:05:55.832: WARN/System.err(2096):     at android.app.Activity.startActivity(Activity.java:2855)
07-16 01:05:55.842: WARN/System.err(2096):     at com.mobilevideoeditor.moved.ShareGalleryView.onOptionsItemSelected(ShareGalleryView.java:79)
07-16 01:05:55.842: WARN/System.err(2096):     at android.app.Activity.onMenuItemSelected(Activity.java:2170)
07-16 01:05:55.842: WARN/System.err(2096):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
07-16 01:05:55.852: WARN/System.err(2096):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
07-16 01:05:55.852: WARN/System.err(2096):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
07-16 01:05:55.852: WARN/System.err(2096):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:525)
07-16 01:05:55.862: WARN/System.err(2096):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
07-16 01:05:55.862: WARN/System.err(2096):     at android.view.View.onTouchEvent(View.java:4179)
07-16 01:05:55.872: WARN/System.err(2096):     开发者_StackOverflow社区at android.widget.TextView.onTouchEvent(TextView.java:6541)
07-16 01:05:55.882: WARN/System.err(2096):     at android.view.View.dispatchTouchEvent(View.java:3709)
07-16 01:05:55.882: WARN/System.err(2096):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-16 01:05:55.882: WARN/System.err(2096):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-16 01:05:55.882: WARN/System.err(2096):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-16 01:05:55.892: WARN/System.err(2096):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-16 01:05:55.902: WARN/System.err(2096):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 01:05:55.912: WARN/System.err(2096):     at android.os.Looper.loop(Looper.java:123)
07-16 01:05:55.912: WARN/System.err(2096):     at android.app.ActivityThread.main(ActivityThread.java:4363)
07-16 01:05:55.922: WARN/System.err(2096):     at java.lang.reflect.Method.invokeNative(Native Method)
07-16 01:05:55.922: WARN/System.err(2096):     at java.lang.reflect.Method.invoke(Method.java:521)
07-16 01:05:55.932: WARN/System.err(2096):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-16 01:05:55.932: WARN/System.err(2096):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-16 01:05:55.932: WARN/System.err(2096):     at dalvik.system.NativeStart.main(Native Method)
07-16 01:05:55.992: WARN/InputManagerService(53): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43cd4d30


The answer is what the stack trace tells you: you have nothing that handles your ACTION_SEND Intent.

Partly, that is because "plain/text" is not a valid MIME type. Try "text/plain". And, if you are going to include a MIME type, you actually need to include an actual email message -- the thing you are claiming is plain text.

See here for an example of using ACTION_SEND.


to avoid the force close dialog window you must implement Error Handling. In the Stack trace you will find the problem, post the Stack Trace.

   public boolean onOptionsItemSelected (MenuItem item){
     try{    
     ...
     ...
     ...    
    }catch(Exception e)
    {
    e.PrintStackTrace();
    } 
      return super.onContextItemSelected(item);
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜