android - 构造函数未定义错误

标签 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

最佳答案

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

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

关于android - 构造函数未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13153466/

相关文章:

delphi - 点击通知后如何返回应用程序?

android - SharedPreferencesChangeListener 不工作

android - 从 Android 生成 PDF

android - 如何在MYsql中建立如下表

javascript - 我想用一个按钮更改文本

button - Twitter 按钮 'url' 参数不包含有效的 URL

android - 如何通过在Android中滑动图像来播放和更改音频

android - 访问 onClickListener 中的变量

java - 如何修改Intent中类的名称(变量而不是硬编码)?

android - 广播接收器最高优先级不工作