无法更改为其他活动

时间:2017-12-06 18:00:19

标签: java android android-activity

我想通过按钮登录从主要活动更改为登录活动。我按​​照所有步骤操作,但在我的设备上测试时仍然无法正常工作。

这是main.java:

公共类MainActivity扩展AppCompatActivity实现了View.OnClickListener {

 public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

  }
}

登录活动是:

12-06 19:57:03.960 6374-6374/com.example.ancaalexandra.proiectandroidd D/ViewRootImpl@c94e23f[MainActivity]: ViewPostImeInputStage processPointer 0
12-06 19:57:03.961 6374-6374/com.example.ancaalexandra.proiectandroidd E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
12-06 19:57:03.961 6374-6374/com.example.ancaalexandra.proiectandroidd V/BoostFramework: BoostFramework() : mPerf = null
12-06 19:57:04.046 6374-6374/com.example.ancaalexandra.proiectandroidd D/ViewRootImpl@c94e23f[MainActivity]: ViewPostImeInputStage processPointer 1
12-06 19:57:04.435 6374-6374/com.example.ancaalexandra.proiectandroidd D/ViewRootImpl@c94e23f[MainActivity]: ViewPostImeInputStage processPointer 0
12-06 19:57:04.522 6374-6374/com.example.ancaalexandra.proiectandroidd D/ViewRootImpl@c94e23f[MainActivity]: ViewPostImeInputStage processPointer 1
12-06 19:57:04.791 6374-6374/com.example.ancaalexandra.proiectandroidd D/ViewRootImpl@c94e23f[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
12-06 19:57:04.837 6374-6374/com.example.ancaalexandra.proiectandroidd D/AndroidRuntime: Shutting down VM
12-06 19:57:04.838 6374-6374/com.example.ancaalexandra.proiectandroidd E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                         Process: com.example.ancaalexandra.proiectandroidd, PID: 6374
                                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ancaalexandra.proiectandroidd/com.example.ancaalexandra.proiectandroidd.LoginActivity}: java.lang.ClassCastException: com.example.ancaalexandra.proiectandroidd.LoginActivity cannot be cast to android.view.View$OnClickListener
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                                                                             at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
                                                                                          Caused by: java.lang.ClassCastException: com.example.ancaalexandra.proiectandroidd.LoginActivity cannot be cast to android.view.View$OnClickListener
                                                                                             at com.example.ancaalexandra.proiectandroidd.LoginActivity.onCreate(LoginActivity.java:19)
                                                                                             at android.app.Activity.performCreate(Activity.java:6956)
                                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                                                                                             at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                             at android.os.Looper.loop(Looper.java:154) 
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) 
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 

Logcat:

Full fence

我在清单文件中声明了两个活动。我做错了什么?谢谢!

2 个答案:

答案 0 :(得分:2)

你处理findViewById()和设置onClick监听器的方式很不寻常。

更改此代码:

DLE-STX-DATA-DLE-ETX-CRC

到此代码:

NAK

它应该可以正常工作。

答案 1 :(得分:1)

您的错误就在这一行:

LoginActivity cannot be cast to android.view.View$OnClickListener

在你的stacktrace中解释:

LoginActivity

您正在尝试将您的活动OnClickListener投射到OnClickListener,但该广告无法正常工作。

如果您的活动实施this,则只需传递class LoginActivity extends Activity implements OnClickListener { protected void onCreate(Bundle savedValues) { ... v.setOnClickListener(this) } }

get