2017-06-27 27 views
-1

我的應用程序崩潰崩潰。應用程序時的登錄按鈕被按下</p> <p>當登錄按鈕被按下警告對話框應該會出現,然後切換到下一個活動在emulator-(機器人工作室)按下登錄按鈕

當「getText.to字符串」語句被刪除

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); 
      } 
     }); 
    } 
} 

BackgroundWorker另一類

這是從點擊登錄按鈕跟蹤日誌它工作正常

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(this);

將其更改爲

BackgroundWorker backgroundWorker = new BackgroundWorker(MainActivity.this); 

它試圖使用該按鈕爲背景

+0

錯誤:(51,111)錯誤:不兼容的類型:MainActivity無法轉換爲OnClickListener – arunberlin

0

你必須編輯BackgroundWorker的類和MainActivity必須實現OnClickListener。編輯的代碼將

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); 

          } 
         } 
); 
} 
} 

如果該項目有多個MainActivity,然後MainActivity前加com.example.cortana.shope2

+0

如果回答了您的問題,請接受它。 – Abhi