如何使文本可点击LinkMovementMethod?

时间:2015-03-05 08:19:56

标签: java android eclipse

我尝试使链接可点击但是在调用方法content_new.setMovementMethod(LinkMovementMethod.getInstance())之后app崩溃; 我不知道该怎么办?!

public View getView(int position, View convertView, ViewGroup parent) {

        parentv = new LinearLayout(mContext);
        parentv.setOrientation(LinearLayout.VERTICAL);
        parentv.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

        if (position == 0) {
            LayoutInflater header = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View itemBox = header.inflate(R.layout.details_news, null);

            share = (ImageView) itemBox.findViewById(R.id.share_icon);
            share.setVisibility(View.VISIBLE);
            share.setClickable(true);
            share.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("text/plain");
                    intent.putExtra(Intent.EXTRA_TEXT,
                            "http://k-islamyah.com/news/i/" + new_detail_id);
                    startActivity(Intent
                            .createChooser(intent, "Share with"));
                }
            });

            new_view = (TextView) itemBox.findViewById(R.id.new_view);
            new_comments = (TextView) itemBox
                    .findViewById(R.id.new_comments);
            new_like = (TextView) itemBox.findViewById(R.id.new_like);
            new_unlike = (TextView) itemBox.findViewById(R.id.new_unlike);
            new_date = (TextView) itemBox.findViewById(R.id.new_date);
            title_details_new = (TextView) itemBox
                    .findViewById(R.id.title_details_new);
            content_new = (TextView) itemBox.findViewById(R.id.content_new);
            image_details_new = (LinearLayout) itemBox
                    .findViewById(R.id.image_details_new);              

            img_details_like = (ImageView) itemBox
                    .findViewById(R.id.img_details_like);
            img_details_dislike = (ImageView) itemBox
                    .findViewById(R.id.img_details_dislike);
            comment_img = (ImageView) itemBox
                    .findViewById(R.id.comment_img);

            comment_img.setClickable(true);


            new_view.setPadding(2, 3, 0, 0);
            new_comments.setPadding(2, 3, 0, 0);
            new_like.setPadding(2, 3, 0, 0);
            new_unlike.setPadding(2, 3, 0, 0);

            int Viewed_num = (Integer.parseInt(Viewed) + 1);
            new_date.setText(CreatedDate);
            new_view.setText(Viewed_num + "");
            new_comments.setText(CommentsCount);
            new_like.setText(Liked);
            new_unlike.setText(UnLiked);
            title_details_new.setText(Title);

            content_new.setText(Html.fromHtml(Contents));
            content_new.setMovementMethod(LinkMovementMethod.getInstance());

            BitmapDrawable background = new BitmapDrawable(bmp);
            image_details_new.setBackgroundDrawable(background);

            parentv.addView(itemBox);
        }



        return parentv;
    }

记录:

03-05 10:14:43.476: E/InputEventReceiver(30658): Exception dispatching input event.
03-05 10:14:43.476: E/MessageQueue-JNI(30658): Exception in MessageQueue callback: handleReceiveCallback
03-05 10:14:43.506: E/MessageQueue-JNI(30658): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.app.ContextImpl.startActivity(ContextImpl.java:995)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.app.ContextImpl.startActivity(ContextImpl.java:962)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.content.ContextWrapper.startActivity(ContextWrapper.java:290)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.text.style.URLSpan.onClick(URLSpan.java:62)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:212)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.widget.TextView.onTouchEvent(TextView.java:7677)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.View.dispatchTouchEvent(View.java:7392)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1983)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1435)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.app.Activity.dispatchTouchEvent(Activity.java:2459)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1931)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.View.dispatchPointerEvent(View.java:7572)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3909)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3804)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3403)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3453)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3422)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3507)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3430)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3564)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3403)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3453)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3422)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3430)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3403)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5445)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5425)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5396)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5519)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.os.MessageQueue.nativePollOnce(Native Method)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.os.MessageQueue.next(MessageQueue.java:133)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.os.Looper.loop(Looper.java:124)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at android.app.ActivityThread.main(ActivityThread.java:5478)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at java.lang.reflect.Method.invokeNative(Native Method)
03-05 10:14:43.506: E/MessageQueue-JNI(30658):  at java.lang.ref
03-05 10:14:43.506: E/AndroidRuntime(30658): FATAL EXCEPTION: main
03-05 10:14:43.506: E/AndroidRuntime(30658): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.app.ContextImpl.startActivity(ContextImpl.java:995)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.app.ContextImpl.startActivity(ContextImpl.java:962)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.content.ContextWrapper.startActivity(ContextWrapper.java:290)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.text.style.URLSpan.onClick(URLSpan.java:62)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:212)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.widget.TextView.onTouchEvent(TextView.java:7677)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.View.dispatchTouchEvent(View.java:7392)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1957)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1983)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1435)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.app.Activity.dispatchTouchEvent(Activity.java:2459)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1931)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.View.dispatchPointerEvent(View.java:7572)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3909)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3804)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3403)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3453)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3422)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3507)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3430)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3564)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3403)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3453)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3422)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3430)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3403)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5445)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5425)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5396)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5519)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.os.MessageQueue.nativePollOnce(Native Method)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.os.MessageQueue.next(MessageQueue.java:133)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.os.Looper.loop(Looper.java:124)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at android.app.ActivityThread.main(ActivityThread.java:5478)
03-05 10:14:43.506: E/AndroidRuntime(30658):    at java.lang.reflect.Method.invokeNative(Native Metho
03-05 10:14:46.156: E/AndroidRuntime(11815): FATAL EXCEPTION: main
03-05 10:14:46.156: E/AndroidRuntime(11815): java.lang.NullPointerException
03-05 10:14:46.156: E/AndroidRuntime(11815):    at khawateer.eslamih.eslamic1712.HomePageActivity.onCreateOptionsMenu(HomePageActivity.java:369)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at android.app.Activity.onCreatePanelMenu(Activity.java:2539)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:413)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:789)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:198)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at android.os.Handler.handleCallback(Handler.java:730)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at android.os.Looper.loop(Looper.java:137)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at android.app.ActivityThread.main(ActivityThread.java:5478)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at java.lang.reflect.Method.invokeNative(Native Method)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at java.lang.reflect.Method.invoke(Method.java:525)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
03-05 10:14:46.156: E/AndroidRuntime(11815):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

你的问题就在这一行:

LayoutInflater header = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

第一个问题是您从非活动上下文中获取LayoutInflater,然后使用它来扩充视图。这导致RuntimeException。您可以在日志中看到它"从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?"

其次,您正在初始化LayoutInflater方法内的getView()。我假设这是一个适配器,因此getView()方法将被调用很多次,这将严重影响列表/网格滚动速度。

解决方案是初始化构造函数本身中的LayoutInflater并使用它在getView()中夸大您的观看次数。

class SampleAdapter extends BaseAdapter {
  private LayoutInflater inflater
  // other stuff
    public SampleAdapter(Context context) {
      // other stuff
      inflater = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
    }

  // other methods

    public View getView(int position, View convertView, ViewGroup parent) {
      // use the inflater object you initialized in the constructor
    }
}