2017-12-06 1 views
0

버튼 활동을 통해 기본 활동에서 로그인 활동으로 변경하고 싶습니다. 모든 단계를 수행했지만 내 장치에서 테스트 할 때 여전히 작동하지 않습니다.다른 활동으로 변경할 수 없습니다.

이 main.java입니다 :

공용 클래스 MainActivity가 AppCompatActivity가 View.OnClickListener를 구현 확장 {

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

    View v = findViewById(R.id.buttonlog); 
    v.setOnClickListener((View.OnClickListener) this); 

} 

@Override 
public void onClick(View v) { 
    if(v.getId()==R.id.buttonlog) 
    { 
     Intent intent = new Intent(this,LoginActivity.class); 
     this.startActivity(intent); 
    } 
}} 

로그인 활동은 다음과 같습니다

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/[email protected][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/[email protected][MainActivity]: ViewPostImeInputStage processPointer 1 
12-06 19:57:04.435 6374-6374/com.example.ancaalexandra.proiectandroidd D/[email protected][MainActivity]: ViewPostImeInputStage processPointer 0 
12-06 19:57:04.522 6374-6374/com.example.ancaalexandra.proiectandroidd D/[email protected][MainActivity]: ViewPostImeInputStage processPointer 1 
12-06 19:57:04.791 6374-6374/com.example.ancaalexandra.proiectandroidd D/[email protected][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)  

매니페스트 파일에서 두 가지 활동을 모두 선언했습니다. 내가 뭘 잘못 했니? 고맙습니다!

답변

1

귀하의 오류가이 라인에 : 당신의 스택 트레이스에 설명되어

v.setOnClickListener((View.OnClickListener) this); 

: 당신은 OnClickListener를로 활동 LoginActivity를 캐스팅하려고

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

은 작동하지 않을 것이다.

당신의 활동이 OnClickListener 방금 ​​this을 전달해야 구현하는 경우 : 처리 findViewById를()와 온 클릭 리스너를 설정하는 것은 매우 드문의

class LoginActivity extends Activity implements OnClickListener { 

    protected void onCreate(Bundle savedValues) { 
     ... 
     v.setOnClickListener(this) 
    } 

} 
+0

효과가있었습니다. 고맙습니다! – Arya1209

2

길을.

변경이 코드 :이 코드에

View v = findViewById(R.id.buttonlog); 
v.setOnClickListener((View.OnClickListener) this); 

는 :

Button loginButton = (Button)findViewById(R.id.buttonlog); 
    loginButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(MainActivity.this,LoginActivity.class); 
startActivity(intent); 
      } 
     }); 

그리고 그것은 잘 작동합니다.

관련 문제