我是android新手。我遇到了上述问题。请帮我解决这个问题。
我的主要活动:
import com.db.appform12.R;
import app.form.appform12.ListUsers;
import app.form.appform12.EditUsers;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button regButton = (Button)findViewById(R.id.buttonMainRegister);
Button listButton = (Button)findViewById(R.id.ButtonMainList);
regButton.setOnClickListener((OnClickListener) this);
listButton.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
Intent intent;
switch(v.getId()) {
case R.id.buttonMainRegister :
intent = new Intent();
intent.setClass(getApplicationContext(), EditUsers.class);
startActivity(intent);
break;
case R.id.ButtonMainList :
intent = new Intent();
intent.setClass(getApplicationContext(), ListUsers.class);
startActivity(intent);
break;
}
}
}
完成LogCat追踪:
12-29 06:23:19.448: D/dalvikvm(2001): Not late-enabling CheckJNI (already on)
12-29 06:23:22.018: I/dalvikvm(2001): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
12-29 06:23:22.018: W/dalvikvm(2001): VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
12-29 06:23:22.018: D/dalvikvm(2001): VFY: replacing opcode 0x6f at 0x0000
12-29 06:23:22.018: I/dalvikvm(2001): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
12-29 06:23:22.018: W/dalvikvm(2001): VFY: unable to resolve virtual method 11358: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
12-29 06:23:22.018: D/dalvikvm(2001): VFY: replacing opcode 0x6f at 0x0000
12-29 06:23:22.068: I/dalvikvm(2001): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
12-29 06:23:22.068: W/dalvikvm(2001): VFY: unable to resolve virtual method 9045: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
12-29 06:23:22.088: D/dalvikvm(2001): VFY: replacing opcode 0x6e at 0x000e
12-29 06:23:22.378: I/dalvikvm(2001): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-29 06:23:22.378: W/dalvikvm(2001): VFY: unable to resolve virtual method 370: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-29 06:23:22.378: D/dalvikvm(2001): VFY: replacing opcode 0x6e at 0x0002
12-29 06:23:22.378: I/dalvikvm(2001): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-29 06:23:22.378: W/dalvikvm(2001): VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getType (I)I
12-29 06:23:22.378: D/dalvikvm(2001): VFY: replacing opcode 0x6e at 0x0002
12-29 06:23:24.038: D/AndroidRuntime(2001): Shutting down VM
12-29 06:23:24.038: W/dalvikvm(2001): threadid=1: thread exiting with uncaught exception (group=0xb0f44648)
12-29 06:23:24.168: E/AndroidRuntime(2001): FATAL EXCEPTION: main
12-29 06:23:24.168: E/AndroidRuntime(2001): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.db.appform12/app.form.appform12.MainActivity}: java.lang.ClassCastException: app.form.appform12.MainActivity cannot be cast to android.view.View$OnClickListener
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.os.Looper.loop(Looper.java:137)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-29 06:23:24.168: E/AndroidRuntime(2001): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 06:23:24.168: E/AndroidRuntime(2001): at java.lang.reflect.Method.invoke(Method.java:525)
12-29 06:23:24.168: E/AndroidRuntime(2001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-29 06:23:24.168: E/AndroidRuntime(2001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 06:23:24.168: E/AndroidRuntime(2001): at dalvik.system.NativeStart.main(Native Method)
12-29 06:23:24.168: E/AndroidRuntime(2001): Caused by: java.lang.ClassCastException: app.form.appform12.MainActivity cannot be cast to android.view.View$OnClickListener
12-29 06:23:24.168: E/AndroidRuntime(2001): at app.form.appform12.MainActivity.onCreate(MainActivity.java:28)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.Activity.performCreate(Activity.java:5133)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-29 06:23:24.168: E/AndroidRuntime(2001): ... 11 more
12-29 06:23:48.758: I/Process(2001): Sending signal. PID: 2001 SIG: 9
12-29 06:25:16.559: I/dalvikvm(2061): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
12-29 06:25:16.629: W/dalvikvm(2061): VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
12-29 06:25:16.629: D/dalvikvm(2061): VFY: replacing opcode 0x6f at 0x0000
12-29 06:25:16.629: I/dalvikvm(2061): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
12-29 06:25:16.649: W/dalvikvm(2061): VFY: unable to resolve virtual method 11358: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
12-29 06:25:16.649: D/dalvikvm(2061): VFY: replacing opcode 0x6f at 0x0000
12-29 06:25:16.699: I/dalvikvm(2061): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
12-29 06:25:16.719: W/dalvikvm(2061): VFY: unable to resolve virtual method 9045: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
12-29 06:25:16.719: D/dalvikvm(2061): VFY: replacing opcode 0x6e at 0x000e
12-29 06:25:16.879: I/dalvikvm(2061): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-29 06:25:16.899: W/dalvikvm(2061): VFY: unable to resolve virtual method 370: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-29 06:25:16.899: D/dalvikvm(2061): VFY: replacing opcode 0x6e at 0x0002
12-29 06:25:16.919: I/dalvikvm(2061): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-29 06:25:16.939: W/dalvikvm(2061): VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getType (I)I
12-29 06:25:16.939: D/dalvikvm(2061): VFY: replacing opcode 0x6e at 0x0002
12-29 06:25:17.979: D/AndroidRuntime(2061): Shutting down VM
12-29 06:25:18.019: W/dalvikvm(2061): threadid=1: thread exiting with uncaught exception (group=0xb0f44648)
12-29 06:25:18.019: E/AndroidRuntime(2061): FATAL EXCEPTION: main
12-29 06:25:18.019: E/AndroidRuntime(2061): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.db.appform12/app.form.appform12.MainActivity}: java.lang.ClassCastException: app.form.appform12.MainActivity cannot be cast to android.view.View$OnClickListener
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.os.Looper.loop(Looper.java:137)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-29 06:25:18.019: E/AndroidRuntime(2061): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 06:25:18.019: E/AndroidRuntime(2061): at java.lang.reflect.Method.invoke(Method.java:525)
12-29 06:25:18.019: E/AndroidRuntime(2061): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-29 06:25:18.019: E/AndroidRuntime(2061): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 06:25:18.019: E/AndroidRuntime(2061): at dalvik.system.NativeStart.main(Native Method)
12-29 06:25:18.019: E/AndroidRuntime(2061): Caused by: java.lang.ClassCastException: app.form.appform12.MainActivity cannot be cast to android.view.View$OnClickListener
12-29 06:25:18.019: E/AndroidRuntime(2061): at app.form.appform12.MainActivity.onCreate(MainActivity.java:25)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.Activity.performCreate(Activity.java:5133)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-29 06:25:18.019: E/AndroidRuntime(2061): ... 11 more
我不知道这些类型的错误。任何建议都是受欢迎的。提前谢谢。
答案 0 :(得分:1)
您应该实施onClickListener
public class MainActivity extends ActionBarActivity implements onClickListener
和@Override onClick(View v)
方法
并注册
regButton.setOnClickListener(this);
listButton.setOnClickListener(this);
答案 1 :(得分:1)
您无法像这样设置OnClickListener
:
regButton.setOnClickListener((OnClickListener) this);
listButton.setOnClickListener((OnClickListener) this);
相反,您需要在活动中实现OnClickListener:
public class MyActivity extends Activity implements OnClickListener{
//..
//then just set this as OnClickListener. and you would need to implement method onClick.
regButton.setOnClickListener(this);
listButton.setOnClickListener(this);
//..
}
答案 2 :(得分:1)
你也可以试试这个,
public class MainActivity extends ActionBarActivity implements onClickListener{
regButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//Your Code..
}
});
}
同样,您可以尝试ListButton
。
答案 3 :(得分:0)
将onClickListener
实施到您的MainActivity class
,如下所示:
public class MainActivity extends ActionBarActivity implements onClickListener{
...
@override
public void onClick(View v) {
//handle your click listeners here.
}
}
答案 4 :(得分:0)
您的代码存在的问题是您将this
投射到OnClickListener
,而您不应该这样做。
按照以下步骤排除错误:
OnClickListener
中删除此广告
即regButton.setOnClickListener(this);
listButton.setOnClickListener(this);
OnClick
方法setOnClickListener
两种方法中都出错,因此将鼠标悬停在它们上方并从弹出式对话框建议中选择最后一个说法"让< { {1}}>实施your_activity_name
" 所以最终的代码如下所示:
OnClickListener
一个关于问题的建议:
最好用小写字母(推荐)NOT资本来定义所有//Your import statements here
public class MainActivity extends ActionBarActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button regButton = (Button) findViewById(R.id.buttonMainRegister);
Button listButton = (Button) findViewById(R.id.ButtonMainList);
regButton.setOnClickListener(this);
listButton.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent;
switch (arg0.getId()) {
case R.id.buttonMainRegister:
intent = new Intent();
intent.setClass(getApplicationContext(), EditUsers.class);
startActivity(intent);
break;
case R.id.ButtonMainList:
intent = new Intent();
intent.setClass(getApplicationContext(), ListUsers.class);
startActivity(intent);
break;
}
}
}
。
所以请将xml views
更改为ButtonMainList
..