我正在做我的计算课程,每次我尝试运行我的应用程序并进行此活动时它崩溃并说不幸的是活动已经停止。如果我从行(-----)向下删除代码的最后一部分,则活动出现并起作用。但有了它,它就无法发挥作用。有谁知道为什么会这样,我怎么能解决它。
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
public class BedroomDoorSigns extends AppCompatActivity {
private String[] wood;
private Spinner spinner;
private String[] colour;
private String[] size;
private Button mButton;
private LinearLayout mLayout;
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_bedroom_door_signs);
wood = getResources().getStringArray(R.array.wood_list);
spinner = (Spinner) findViewById(R.id.wood_spinner);
colour = getResources().getStringArray(R.array.colour_list);
spinner = (Spinner) findViewById(R.id.colour_spinner);
size = getResources().getStringArray(R.array.size_list);
spinner = (Spinner) findViewById(R.id.size_spinner);
Button Buttons = (Button) findViewById(R.id.Cart);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mEditText = (EditText) findViewById(R.id.editText);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, wood);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colour);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter2);
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, size);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter3);
}
public View.OnClickListener onClick() {
return new View.OnClickListener() {
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
public TextView createNewTextView(String text) {
final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("BASKET: " + text);
return textView;
}
}
谢谢
答案 0 :(得分:2)
试试这个
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
});
答案 1 :(得分:0)
java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)&#39;在com.example.emily.activity414timber.BedroomDoorSigns.onCreate(BedroomDoorSigns.java:41)的空对象引用上
您的按钮对象(mButton)为空,因此它会抛出&#39; nullpointerexception&#39;当你打电话给&#39; setOnclickListner&#39;该对象的方法