我正在閱讀教程,教會我如何開發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;
}
}
}
logcat的/錯誤的堆棧跟蹤? – 2014-09-13 06:28:07