2017-06-27 15 views
-1

マイアプリのクラッシュを表示され、その後、次のアクティビティに切り替える必要があります。 「getText.to列」文はBackgroundWorkerこれは、ログインボタンをクリックしてからトレースログがあるアプリ(アンドロイドスタジオ)

別のクラスがある

public class MainActivity extends AppCompatActivity { 

    EditText id_txt,password_txt; 

    ImageButton lgin; 
    private AlphaAnimation signupclick = new AlphaAnimation(1F, 0.1F); 
    private AlphaAnimation loginclick = new AlphaAnimation(1F, 0.5F); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     setContentView(R.layout.activity_main); 

     id_txt = (EditText)findViewById(R.id.id) ; 
     password_txt = (EditText)findViewById(R.id.pass); 

     onClickButtonListner(); 
    } 

    public void onClickButtonListner() { 
     lgin = (ImageButton).findViewById(R.id.login_btn); 
     lgin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       v.startAnimation(loginclick); 
       String id_string = id_txt.getText().toString(); 
       String password_string = password_txt.getText().toString(); 
       String type = "login"; 
       Intent intent = new Intent(com.example.cortana.shope2.MainActivity.this,com.example.cortana.shope2.search.class); 
       startActivity(intent); 
       BackgroundWorker backgroundWorker = new BackgroundWorker(this); 
       backgroundWorker.execute(type,id_string,password_string); 
      } 
     }); 
    } 
} 

削除されたとき

は、それが正常に動作します

java.lang.ClassCastException: com.example.cortana.shope2.MainActivity$1 cannot be cast to android.content.Context at com.example.cortana.shope2.BackgroundWorker.(BackgroundWorker.java:30)
at com.example.cortana.shope2.MainActivity$1.onClick(MainActivity.java:52) at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method)

+1

トレースログを貼り付けてください。 – fbwnd

答えて

0

BackgroundWorkerクラスのコンストラクタにContextが必要な場合は、次のように記述してください。

BackgroundWorker backgroundWorker = new BackgroundWorker(MainActivity.this); 
+0

まだアプリがクラッシュする – arunberlin

+0

は同じエラーですか? – csabapap

0

クラッシュがこのラインでcasuedさ:

BackgroundWorker backgroundWorker = new BackgroundWorker(MainActivity.this); 

からBackgroundWorker backgroundWorker = new BackgroundWorker(this);

変更して、あなたがのBackgroundWorkerクラスを編集しなければなりませんそのコンテキストとして、ボタンを使用する

+0

エラー:(51,111)エラー:互換性のないタイプ:MainActivityをOnClickListenerに変換できません – arunberlin

0

をしようとしていますMainActivityはOnClickListenerを実装する必要があります。複数MainActivityがプロジェクト内にある場合は、編集コードは、MainActivity前com.example.cortana.shope2を追加し、

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

EditText id_txt,password_txt; 

ImageButton lgin; 
private AlphaAnimation signupclick = new AlphaAnimation(1F, 0.1F); 
private AlphaAnimation loginclick = new AlphaAnimation(1F, 0.5F); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
WindowManager.LayoutParams.FLAG_FULLSCREEN); 
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
setContentView(R.layout.activity_main); 

id_txt = (EditText)findViewById(R.id.id) ; 
password_txt = (EditText)findViewById(R.id.pass); 

onClickButtonListner(); 

} 

public void onClickButtonListner() 
{ 

lgin = (ImageButton).findViewById(R.id.login_btn); 
lgin.setOnClickListener(new View.OnClickListener() 
         { 
          @Override 
          public void onClick(View v) 
          { 
           v.startAnimation(loginclick); 
           String id_string = id_txt.getText().toString(); 
           String password_string = password_txt.getText().toString(); 
           String type = "login"; 
           Intent intent = new Intent(com.example.cortana.shope2.MainActivity.this,com.example.cortana.shope2.search.class); 
           startActivity(intent); 
           BackgroundWorker backgroundWorker = new BackgroundWorker(MainActivity.this); 
           backgroundWorker.execute(type,id_string,password_string); 

          } 
         } 
); 
} 
} 

になります。

+0

これがあなたの質問に答えた場合はそれを受け入れてください。 – Abhi

関連する問題