2012-10-31 111 views
1

我在Main.java类中编写了此代码,但程序未运行。为什么?该程序有错误Intent i=new Intent(this,Location.class);有什么问题?构造函数未定义错误

package org.example.loyaltier; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.content.Intent; 

public class Main extends Activity { 

private WebView mWebView; 
private Button mHome; 
private Button mProduct; 
private Button mPlaces; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mWebView = (WebView)findViewById(R.id.web_view); 

    mHome = (Button)findViewById(R.id.button_home); 
    mProduct=(Button)findViewById(R.id.button_product); 
    mPlaces=(Button)findViewById(R.id.button_places); 

    mHome.setOnClickListener(onClickListener); 


} 
private OnClickListener onClickListener=new OnClickListener(){ 
    public void onClick(View v){ 
     switch(v.getId()){ 
     case R.id.button_home: 
      mWebView.loadUrl  ("http://loyaltier.com/app/mobile/code/home/home.php"); 
      break; 
     case R.id.button_product: 
      mWebView.loadUrl("http://loyaltier.com/app/mobile/design/catalog/catalog1.php"); 
      break; 
     case R.id.button_places: 
      Intent i=new Intent(this,Location.class); 
      startActivity(i); 
      break; 
     } 
    } 
}; 
} 

的问题是:The constructor Intent(new View.OnClickListener(){}, Class<Location>) is undefined但我不明白这是什么问题?如何解决这个问题?

请帮我;)

的logcat ::

10-31 11:35:04.135: D/AndroidRuntime(16626): Shutting down VM 
10-31 11:35:04.135: W/dalvikvm(16626): threadid=1: thread exiting with uncaught exception (group=0x40a3e1f8) 
10-31 11:35:04.159: E/AndroidRuntime(16626): FATAL EXCEPTION: main 
10-31 11:35:04.159: E/AndroidRuntime(16626): java.lang.IllegalStateException: Could not find a method Places(View) in the activity class org.example.loyaltier.Main for onClick handler on view class android.widget.Button with id 'button_places' 
10-31 11:35:04.159: E/AndroidRuntime(16626): at android.view.View$1.onClick(View.java:3026) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at android.view.View.performClick(View.java:3480) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at android.view.View$PerformClick.run(View.java:13983) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at android.os.Handler.handleCallback(Handler.java:605) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at android.os.Looper.loop(Looper.java:137) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at android.app.ActivityThread.main(ActivityThread.java:4340) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at java.lang.reflect.Method.invokeNative(Native Method) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at java.lang.reflect.Method.invoke(Method.java:511) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at dalvik.system.NativeStart.main(Native Method) 
10-31 11:35:04.159: E/AndroidRuntime(16626): Caused by: java.lang.NoSuchMethodException: Places [class android.view.View] 
10-31 11:35:04.159: E/AndroidRuntime(16626): at java.lang.Class.getConstructorOrMethod(Class.java:460) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at java.lang.Class.getMethod(Class.java:915) 
10-31 11:35:04.159: E/AndroidRuntime(16626): at android.view.View$1.onClick(View.java:3019) 
10-31 11:35:04.159: E/AndroidRuntime(16626): ... 11 more 
10-31 11:35:04.190: D/dalvikvm(16626): GC_CONCURRENT freed 212K, 3% free 14282K/14599K, paused 2ms+2ms 
10-31 11:35:09.057: I/Process(16626): Sending signal. PID: 16626 SIG: 9 

回答

1

thisnew Intent(this,Location.class);指匿名View.OnClickListener对象的实例。您需要提供Context的实例。试试:

Intent i=new Intent(Main.this,Location.class); 
+0

感谢您的回答。我将此更改为Main.this,但是当我单击地点按钮或产品按钮时,程序停止。为什么?我在第一篇文章中写了LogCat。 –

+0

@PariyaDaghoghi那是另一个问题 – Blackbelt

+0

你的意思是我在另一个页面问过这个问题? –