Java 设置onclick时强制关闭
我创建了一个按钮,并通过Java 设置onclick时强制关闭,java,android,Java,Android,我创建了一个按钮,并通过 remove.setId(0 + count); 然后我试着把这个按钮放在我的onCreate包中使用它 Button remove = (Button)findViewById(1); remove.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method
remove.setId(0 + count);
然后我试着把这个按钮放在我的onCreate包中使用它
Button remove = (Button)findViewById(1);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
当我在oncreate中创建按钮remove时,它会运行,但当我添加onclick侦听器时,我没有收到任何错误,但当我运行它时,它会强制关闭
日志
这是我的完整档案
package com.th3ramr0d.armytrooptotask;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
public int count = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addTroopBtn = (Button) findViewById(R.id.addTroop);
addTroopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(count <= 5)
{
inputName(v);
}
else
{
}
}
});
Button remove = (Button)findViewById(1);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
public void inputName(final View v){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
//alert.setTitle("Enter Name and Rank");
alert.setMessage("Please Enter A Name");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable name = input.getText();
addTroop(name);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
public void addTroop(Editable name){
LinearLayout mainPage = (LinearLayout) findViewById(R.id.manageTroopsMain);
if (count <= 5)
{
//CREATE NEW LINEAR LAYOUT
LinearLayout addTroopLayout = new LinearLayout(this);
//CREATE LAYOUT PARAMS FOR LAYOUT
LinearLayout.LayoutParams newLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newLayout.bottomMargin = 10;
//STYLE NEW LINEAR LAYOUT
addTroopLayout.setTag("addTroopLayout" + count);
addTroopLayout.setLayoutParams(newLayout);
addTroopLayout.setOrientation(LinearLayout.HORIZONTAL);
//CREATE NEW BUTTONS
Button newTroop = new Button(this);
Button remove = new Button(this);
Button change = new Button(this);
//CREATE LAYOUT PARAMS FOR BUTTONS
LinearLayout.LayoutParams newTroopParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 20f);
LinearLayout.LayoutParams rmvBtnParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
LinearLayout.LayoutParams chngNameParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
//STYLE NEW BUTTONS
newTroop.setText(name);
newTroop.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
newTroop.setLayoutParams(newTroopParam);
remove.setId(0 + count);
remove.setText("-");
remove.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
remove.setLayoutParams(rmvBtnParam);
change.setText("...");
change.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
change.setLayoutParams(chngNameParam);
//ADD VIEWS TO NEW LAYOUT
addTroopLayout.addView(newTroop);
addTroopLayout.addView(remove);
addTroopLayout.addView(change);
//ADD NEW LAYOUT TO mainPage LAYOUT
mainPage.addView(addTroopLayout);
//Increment Counter
count++;
}
}
}
package com.th3ramr0d.armytrooptostask;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.os.Bundle;
导入android.text.Editable;
导入android.view.Gravity;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup.LayoutParams;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.LinearLayout;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
公共整数计数=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮AddTroPBTN=(按钮)findViewById(R.id.AddTourse);
AddTroPBTN.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
如果(计数这是您的问题:
按钮移除=(按钮)findViewById(1)
这应该是这样的:
Button remove = (Button)findViewById(R.id.buttonRemove); // that should be a long int
坠机可能发生在这条线路上:
Button remove = (Button)findViewById(1);
您需要为膨胀布局中的按钮提供有效ID。值1
没有意义。Button remove=(Button)findViewById(1);
可能返回null。您说您正在代码的前几行设置视图的ID,但我在列表中的任何地方都看不到您提供的“完整文件”。您可能在完整文件中缺少代码,或者您从未设置视图的ID。无论哪种方式,您都可以提供xml吗?在尝试使用您所说的ID获取按钮ID后,您正在设置删除按钮ID。(该顺序不正确…)那么我该如何重新排序呢?我需要能够在创建这些按钮后单击它们。那么setId的意义是什么?我应该使用什么才能使用有效的id?View.setId()
仅在通过调用new
创建视图时使用。如果从布局中展开视图
(这是setContentView()
为您所做的)然后通过XML布局文件设置ID。如果未在XML中设置android:ID
字段,则无法在充气后按ID检索视图。
Button remove = (Button)findViewById(1);