构造函数未定义错误

时间:2012-10-31 07:54:35

标签: android button android-intent

我在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 个答案:

答案 0 :(得分:1)

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

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