我是 android 初学者,我正在尝试继承基础知识中的这段代码,以从 void 函数类 A 获取消息并使其显示,但我不能......显示这条消息 java.lang.nullpointerexeption ......这个是A类和主要活动的鳕鱼......谢谢:
一个.java:
public class A extends Activity {
public void showA(){
Toast.makeText(this, "Class A", Toast.LENGTH_SHORT).show();
}
}
MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button show_a=(Button)findViewById(R.id.Show_A);
show_a.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
A c=new A();
c.showA();
}
catch(Exception e){
Toast.makeText(getApplicationContext(), e+"", Toast.LENGTH_LONG).show();
}
}
});
}