使用 onClickView 调用另一个类

发布于 2024-10-16 02:43:12 字数 1342 浏览 5 评论 0原文

我想使用单击按钮将选择参数传递给另一个类,该类将使用传递的参数构建地图屏幕。我专注于让按钮动作发挥作用。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧幽蓝 2024-10-23 02:43:12

为了扩展@Heiko Rupp的答案,如果你想让Class2显示地图,它需要扩展Activity之类的东西。因此,您不能仅使用普通方法来调用它。您需要在清单中注册Activity,然后使用Intent调用它。以下是您应该做的事情的示例:

public class Class1 extends Activity implements OnClickListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        Button button = (Button)findViewById(R.id.btn_configup1);
        button.setOnClickListener(this);        
   }

    public void onClick(View v) {
        Intent intent = new Intent(Class1.this,Class2.class);
        intent.putExtra("key","data");
        ...
        startActivity(intent);
    }
}

public class Class2  extends MapActivity {

    String mData;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mData = extras.getString("key");
            ...
        }
        ...
    }
}

我还可以建议您使用比 Class1Class2 更具描述性的类名称吗?

To expand on @Heiko Rupp's answer, if you want Class2 to display a map, it needs to extend something like Activity. As such, you can't just call it with a normal method. You need to register the Activity in your manifest and then call it using an Intent. Here is a sample of the kind of thing you should be doing:

public class Class1 extends Activity implements OnClickListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        Button button = (Button)findViewById(R.id.btn_configup1);
        button.setOnClickListener(this);        
   }

    public void onClick(View v) {
        Intent intent = new Intent(Class1.this,Class2.class);
        intent.putExtra("key","data");
        ...
        startActivity(intent);
    }
}

public class Class2  extends MapActivity {

    String mData;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mData = extras.getString("key");
            ...
        }
        ...
    }
}

Can I also suggest that you use more descriptive class names than Class1 and Class2.

若水微香 2024-10-23 02:43:12

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 via new 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 within execMapBuild().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文