Try this-
Intent myIntentActivity1 = new Intent(mainlogin.this, ReadComments.class);
startActivity(myIntentActivity1);
Or-
Intent myIntentActivity1 = new Intent(getBaseContext(), ReadComments.class);
startActivity(myIntentActivity1);
質問
I have this main page where I have set multiple webViews. Basically the idea is simple. I use three webViews and all of them take different content from an online website. The moment they click on the web view, it should take them to another activity. Then leave the rest to me. I just need help with linking the web view to another activity using the onTouchListener.
Here is my Java File: Look for the // THE CODE FOR THE OnTOUCHEVETN STARTS HERE!
line. That is where I am trying my webview 1 to start a second activity. I am trying to use this: Intent myIntentActivity1 = new Intent(this, ReadComments.Class);
but java gives me the following error:
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;
}
}
}
Here is my XML file:
<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>
What could be causing this error?
解決
Try this-
Intent myIntentActivity1 = new Intent(mainlogin.this, ReadComments.class);
startActivity(myIntentActivity1);
Or-
Intent myIntentActivity1 = new Intent(getBaseContext(), ReadComments.class);
startActivity(myIntentActivity1);