当我单击activity_main.xml中显示的按钮时,我无法启动我的活动(显示消息)。我已经尝试解决onClick中的问题,但仍然无法运行它。我能知道是什么问题吗?谢谢。
以下是代码:
MainActivity.java
Data is created to represent just `12` columns of the dataset mentioned by OP.
set.seed(1)
dataset <- data.frame(col1 = sample(1:10,10),
col2 = sample(1:10,10),
col3 = sample(1:10,10),
col4 = sample(1:10,10),
col5 = sample(11:20,10),
col6 = sample(11:20,10),
col7 = sample(11:20,10),
col8 = sample(11:20,10),
col9 = sample(21:30,10),
col10 = sample(21:30,10),
col11 = sample(21:30,10),
col12 = sample(21:30,10)
)
dataset
# col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12
# 1 3 3 10 5 19 15 20 14 25 23 27 30
# 2 4 2 2 6 16 18 13 18 27 21 24 27
# 3 5 6 6 4 17 14 14 13 24 26 23 23
# 4 7 10 1 2 14 12 19 19 23 27 30 24
# 5 2 5 9 10 18 11 18 17 30 25 29 21
# 6 8 7 8 8 20 16 12 15 22 24 22 26
# 7 9 8 7 9 11 17 15 20 29 22 21 28
# 8 6 4 5 1 12 19 17 12 21 28 25 25
# 9 10 1 3 7 13 13 11 16 28 30 28 29
# 10 1 9 4 3 15 20 16 11 26 29 26 22
activity_main.xml
public class MainActivity extends AppCompatActivity{
public Button but1;
public void init(){
but1= (Button)findViewById(R.id.button1);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent display = new Intent(MainActivity.this, DisplayMessage.class);
startActivity(display);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.products));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdapter);
}
}
答案 0 :(得分:2)
似乎您从未从init()
方法中调用onCreate()
。因此,but1
从未被初始化,OnClickListener
从未被设置。