2014-09-13 26 views
0

我正在閱讀教程,教會我如何開發Android應用。我一直在經歷這一切,但由於某種原因,當我運行此活動時,應用程序關閉並且說它已停止工作。據我所知,我已經正確複製了該人的代碼,但他的作品。 我發現,問題是裏面的onCreate行,說tryCmd.setOnClickListener(this);Android應用因button.setOnClickListener(this)而關閉

如果我註釋掉單行線,活動正常工作(但是當我按一下按鈕,顯然只是沒有做任何事情)。該應用程序與togglebutton的onClickListener語句正常工作。誰能告訴我什麼是錯的?謝謝。

這裏是我的activity.java所有代碼:

package com.example.testapp; 

import java.util.Random; 

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.text.InputType; 
import android.view.Gravity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.ToggleButton; 

public class Text extends Activity implements View.OnClickListener{ 

    EditText input; 
    Button tryCmd; 
    ToggleButton passTog; 
    TextView display; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.text); 
     //Call the method to assign variables to the elements. 
     assignVariables(); 
     //Initialize the button and ToggleButton to work with the onClick method. 
     passTog.setOnClickListener(this); 
     tryCmd.setOnClickListener(this);   

    } 

    private void assignVariables() { 
     // TODO Auto-generated method stub 
     input = (EditText) findViewById(R.id.etCommands); 
     Button tryCmd = (Button) findViewById(R.id.bResults); 
     passTog = (ToggleButton) findViewById(R.id.tbPassword); 
     display = (TextView) findViewById(R.id.tvResults); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     switch(v.getId()){ 
     case R.id.bResults: 
      String check = input.getText().toString(); 
      if (check.contentEquals("left")){ 
       display.setGravity(Gravity.LEFT); 
      }else if(check.contentEquals("center")){ 
       display.setGravity(Gravity.CENTER); 
      }else if(check.contentEquals("right")){ 
       display.setGravity(Gravity.RIGHT); 
      }else if(check.contentEquals("blue")){ 
       display.setTextColor(Color.BLUE); 
      }else if(check.contains("WTF")){ 
       Random crazy = new Random(); 
       display.setText("WTF!?!?"); 
       display.setTextSize(crazy.nextInt(75)); 
       display.setTextColor(Color.rgb(crazy.nextInt(255), crazy.nextInt(255), crazy.nextInt(255))); 
       switch(crazy.nextInt(3)){ 
       case 0: 
        display.setGravity(Gravity.LEFT); 
        break; 
       case 1: 
        display.setGravity(Gravity.CENTER);      
        break; 
       case 2: 
        display.setGravity(Gravity.RIGHT);      
        break; 
       } 
      }else{ 
       display.setText("invalid"); 
       display.setTextColor(Color.BLACK); 
       display.setGravity(Gravity.CENTER); 
      } 

      //Clear the input box if it doesn't contain WTF. 
      if(!check.contains("WTF")){ 
       input.setText(""); 
      } 
      break; 

     case R.id.tbPassword: 
      if (passTog.isChecked()){ 
       input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
      } else{ 
       input.setInputType(InputType.TYPE_CLASS_TEXT); 
      } 
      break; 
     } 
    } 

} 
+0

logcat的/錯誤的堆棧跟蹤? – 2014-09-13 06:28:07

回答

0

沒有與此線的問題..

Button tryCmd = (Button) findViewById(R.id.bResults); 

要創建局部變量和分配值,以新的局部變量... ...因此,全局變量仍然NULL ..

因此從中刪除該行assignVari ABLES()函數 ..

試試這個代碼...

private void assignVariables() { 
    // TODO Auto-generated method stub 
    input = (EditText) findViewById(R.id.etCommands); 
    tryCmd = (Button) findViewById(R.id.bResults); 
    passTog = (ToggleButton) findViewById(R.id.tbPassword); 
    display = (TextView) findViewById(R.id.tvResults); 
} 
+0

工作,謝謝。 – 2014-09-13 06:36:29

+0

我試圖這樣做,但它不會讓我5分鐘。現在完成了,謝謝。 – 2014-09-13 06:41:38

+0

很高興幫助.. – 2014-09-13 06:42:04

1

你是初始化tryCmd爲局部變量。 並設置onClickListner onglobal變量tryCmd

刪除按鈕從該行

Button tryCmd = (Button) findViewById(R.id.bResults); 

,並改變它

tryCmd = (Button) findViewById(R.id.bResults); 
+0

就是這樣!謝謝。 – 2014-09-13 06:36:04

1

定義tryCmd爲局部變量,因此只檢測assignVariables() method.you應該將其更改爲全局變量(如您所定義但不使用)

private void assignVariables() { 
    // TODO Auto-generated method stub 
    input = (EditText) findViewById(R.id.etCommands); 
    Button tryCmd = (Button) findViewById(R.id.bResults);//error in this line you define it as local variable delete Button before it 
    passTog = (ToggleButton) findViewById(R.id.tbPassword); 
    display = (TextView) findViewById(R.id.tvResults); 
} 

編輯:

private void assignVariables() { 
    // TODO Auto-generated method stub 
    input = (EditText) findViewById(R.id.etCommands); 
    tryCmd = (Button) findViewById(R.id.bResults); 
    passTog = (ToggleButton) findViewById(R.id.tbPassword); 
    display = (TextView) findViewById(R.id.tvResults); 
} 
+0

你釘了它,謝謝! – 2014-09-13 06:36:51

相關問題