2013-12-19 23 views
2

我有這個主頁面,我設置了多個webViews。基本上這個想法很簡單。我使用三個webViews,他們都從一個在線網站獲取不同的內容。當他們點擊網絡視圖時,應該將他們帶到另一個活動。然後把剩下的留給我。我只需要使用onTouchListener將web視圖鏈接到另一個活動。使用WebView onTouchListener鏈接到另一個活動 - Android

這是我的Java文件:查找// THE CODE FOR THE OnTOUCHEVETN STARTS HERE!行。那就是我嘗試使用我的webview 1開始第二個活動的地方。我試圖用這個:Intent myIntentActivity1 = new Intent(this, ReadComments.Class);但Java給了我以下錯誤:

Remove arguments to match '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> 

什麼導致這個錯誤?

回答

2

嘗試這個 -

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

或 -

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

而在這行你正在使用。類,但它的.class與小C字母開頭。 java區分大小寫。 –

+0

哦。我的。神。非常感謝你:) 我完全忘了getBaseContext()。這解決了整個問題!你是最好的:) –

+0

歡迎你:) –

相關問題