使用 onClickView 调用另一个类
我想使用单击按钮将选择参数传递给另一个类,该类将使用传递的参数构建地图屏幕。我专注于让按钮动作发挥作用。我使用 onCLickListener 和 onCLickView 如下
Class1:
public class Class1 extends Activity implements OnClickListener {
Class2 class2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
..........
Button button = (Button)findViewById(R.id.btn_configup1);
button.setOnClickListener(this);
}
public void onClick(View v) {
Class2 class2 = new Class2();
//Save state.. selections and params and use bundle
//to pass into class2
class2.execMapBuild();
}
}
Class2:
public class Class2 extends MapActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.drawable.navup);
}
public void execMapBuild() {
finish(); //just in case we return.
Intent intent = new Intent(CLass2.this, Class2.class);
startActivity(intent);
}
除了所需的按钮操作之外,我一切正常。我希望 Class1.onVlickView 中的按钮单击使用按钮单击操作来调用 Class2.execMapBuild。我单击按钮捕获操作并调用 Class2 上的 execMapBuild 方法。但是当它从 startActivity(intent) 移动到 onCreate 时,我得到一个 NullPointerException。
我已经尝试了其他几种方法来解决这个问题,但这似乎是最好的,而且我似乎即将弄清楚。我真的很感激对我可能缺少的内容的解释。 添加了最初未复制的代码。
I want to use a button click to pass selection parameters to another class that will build a map screen using the passed parameters. I am focused on getting my button action working. I a using onCLickListener and onCLickView as follows
Class1:
public class Class1 extends Activity implements OnClickListener {
Class2 class2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
..........
Button button = (Button)findViewById(R.id.btn_configup1);
button.setOnClickListener(this);
}
public void onClick(View v) {
Class2 class2 = new Class2();
//Save state.. selections and params and use bundle
//to pass into class2
class2.execMapBuild();
}
}
Class2:
public class Class2 extends MapActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.drawable.navup);
}
public void execMapBuild() {
finish(); //just in case we return.
Intent intent = new Intent(CLass2.this, Class2.class);
startActivity(intent);
}
I have everything working except the desired button action. I want the button click in Class1.onVlickView to call Class2.execMapBuild using the button click action. I have the button click capturing the action and calling the execMapBuild method on Class2. But I get a NullPointerException as it moves from startActivity(intent) into onCreate.
I have tried several other ways of nailing this down, but this seems the best and I seem close to figuring it out. I would really appreciate an explanation of what I may be missing.
Added code that was initially not copied in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了扩展@Heiko Rupp的答案,如果你想让
Class2
显示地图,它需要扩展Activity
之类的东西。因此,您不能仅使用普通方法来调用它。您需要在清单中注册Activity
,然后使用Intent
调用它。以下是您应该做的事情的示例:我还可以建议您使用比
Class1
和Class2
更具描述性的类名称吗?To expand on @Heiko Rupp's answer, if you want
Class2
to display a map, it needs to extend something likeActivity
. As such, you can't just call it with a normal method. You need to register theActivity
in your manifest and then call it using anIntent
. Here is a sample of the kind of thing you should be doing:Can I also suggest that you use more descriptive class names than
Class1
andClass2
.Class2
没有 Activity,因此系统不会调用 Activity 的回调。如果它是一个 Activity,您不能仅通过 new Class2() 调用它,因为回调仍然不会执行。
尝试清理此问题,然后使用
Intent
从 Class1 启动 Class2 活动,就像您在execMapBuild()
中所做的那样。Class2
is no activity, so the callbacks of an Activity will not be called by the system.And if it were an
Activity
, you could not just call into it vianew Class2()
, as still the callbacks are not executed.Try to clean this up and then start Class2 activity from Class1 with an
Intent
as you are doing withinexecMapBuild()
.