【问题标题】:Linking to another Activity using WebView onTouchListener - Android使用 WebView onTouchListener 链接到另一个 Activity - Android
【发布时间】:2025-12-05 15:05:01
【问题描述】:

我有这个主页,我在其中设置了多个 webView。基本上这个想法很简单。我使用三个 webView,它们都从在线网站获取不同的内容。当他们点击网络视图时,它应该将他们带到另一个活动。然后剩下的交给我。我只需要使用 onTouchListener 将 Web 视图链接到另一个活动的帮助。

这是我的 Java 文件:查找 // THE CODE FOR THE OnTOUCHEVETN STARTS HERE! 行。这就是我正在尝试我的 webview 1 开始第二个活动的地方。我正在尝试使用它:Intent myIntentActivity1 = new Intent(this, ReadComments.Class); 但 java 给了我以下错误:

删除参数以匹配 'Intent()'

package com.testapp;

import com.testapp.R.menu;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;

public class mainlogin extends Activity implements OnClickListener{

private Button  mRegister, mlogin, mcontactus;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainlogin);


    ImageView img = (ImageView)findViewById(R.id.centennialmsssite);
    img.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("http://mywebsite.com"));
            startActivity(intent);
        }

    });

    WebView wv = (WebView)findViewById(R.id.my_webview);
    wv.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);


            return true;
            }
    });

    wv.loadUrl("http://mywebsite.com/main-content");

    // THE CODE FOR THE OnTOUCHEVETN STARTS HERE!

    WebView wv1 = (WebView)findViewById(R.id.topic_one);
    wv1.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);


            return true;
            }
    });

    wv1.loadUrl("http://mywebsite.com/content-one");

    wv1.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP){

                Intent myIntentActivity1 = new Intent(this, ReadComments.Class);
                //call N_X and wait for result
                startActivity(myIntentActivity1);

                return true;
            }
            return false;
        }
    });

    // IT ENDS HERE!

    WebView wv2 = (WebView)findViewById(R.id.topic_two);
    wv2.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);


            return true;
            }
    });

    wv2.loadUrl("http://mywebsite.com/content-two");

    WebView wv3 = (WebView)findViewById(R.id.topic_three);
    wv3.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);


            return true;
            }
    });

    wv3.loadUrl("http://mywebsite.com/content-three");

    mRegister = (Button)findViewById(R.id.btn_register);
    mRegister.setOnClickListener(this);

    mlogin = (Button)findViewById(R.id.btn_login);
    mlogin.setOnClickListener(this);

    mcontactus = (Button) findViewById(R.id.btn_contact_us);
    mcontactus.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btn_login:
        Intent i = new Intent(this, Login.class);
        startActivity(i);
        break;
    case R.id.btn_register:
        Intent i1 = new Intent(this, Register.class);
        startActivity(i1);
        break;
    case R.id.btn_contact_us:
        Intent i2 = new Intent(this, contactus.class);
        startActivity(i2);
        break;
    default:
        break;
    }

}
}

这是我的 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >

<Button
    android:id="@+id/btn_register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btn_login"
    android:layout_alignBottom="@+id/btn_login"
    android:layout_alignRight="@+id/textView1"
    android:background="#333333"
    android:minHeight="40dp"
    android:minWidth="140dp"
    android:text="@string/mainpage_register_button"
    android:textColor="#999999" />

<Button
    android:id="@+id/btn_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="280dp"
    android:background="#333333"
    android:minHeight="40dp"
    android:minWidth="140dp"
    android:text="@string/main_button_login"
    android:textColor="#999999" />

<Button
    android:id="@+id/btn_contact_us"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btn_login"
    android:layout_alignRight="@+id/btn_register"
    android:layout_below="@+id/btn_register"
    android:background="#333333"
    android:minHeight="40dp"
    android:layout_marginTop="2dp"
    android:text="@string/mainpage_contact_us"
    android:textColor="#999999" />

<ImageView
    android:id="@+id/centennialmsssite"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btn_contact_us"
    android:layout_below="@+id/btn_contact_us"
    android:layout_marginTop="14dp"
    android:contentDescription="@string/mss_button_description"
    android:src="@drawable/msswebsitebutton" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="286dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:gravity="center"
    android:text="@string/welcome_mainpage"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffffff"
    android:textStyle="bold" />

<WebView
    android:id="@+id/my_webview"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignRight="@+id/sitebutton"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="12dp"
    android:maxHeight="40dp"
    android:minHeight="40dp" />

<WebView
    android:id="@+id/topic_one"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignLeft="@+id/my_webview"
    android:layout_alignRight="@+id/my_webview"
    android:layout_below="@+id/my_webview"
    android:layout_marginTop="5dp"
    android:maxHeight="70dp"
    android:minHeight="70dp" />

<WebView
    android:id="@+id/topic_two"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignLeft="@+id/topic_one"
    android:layout_alignRight="@+id/topic_one"
    android:layout_below="@+id/topic_one"
    android:maxHeight="70dp"
    android:minHeight="70dp" />

    <WebView
        android:id="@+id/topic_three"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignLeft="@+id/topic_two"
        android:layout_alignRight="@+id/topic_two"
        android:layout_below="@+id/topic_two"
        android:maxHeight="70dp"
        android:minHeight="70dp" />

</RelativeLayout>

什么可能导致这个错误?

【问题讨论】:

    标签: java android android-intent webview ontouchlistener


    【解决方案1】:

    试试这个-

    Intent myIntentActivity1 = new Intent(mainlogin.this, ReadComments.class);
    startActivity(myIntentActivity1);
    

    或者-

    Intent myIntentActivity1 = new Intent(getBaseContext(), ReadComments.class);
    startActivity(myIntentActivity1);
    

    【讨论】:

    • 在该行中您使用的是 .Class 但它是 .class 以小 c 字母开头。 java 区分大小写。
    • 哦。我的。上帝。非常感谢 :) 我完全忘记了 getBaseContext()。这解决了整个问题!你是最棒的:)
    最近更新 更多