Intent 在 onClickListener 中不起作用?

发布于 2024-12-02 04:49:28 字数 3113 浏览 0 评论 0原文

在我编写的代码中,我放置了在 onclicklistener 中打开新活动的意图,但它不起作用。侦听器中的其他代码也可以工作,如果我将相同的意图放在侦听器外部的“备用位置”中,它也可以工作。所以我不确定这里出了什么问题。我真的很感激任何帮助,谢谢。

这是选项卡活动中使用的代码,其中包含为其编写侦听器的按钮。

public class RemaindersPage extends Activity
{
    com.commsware.pgtracker.RemainderControl rc1;
    int count;
    LinearLayout.LayoutParams lp1,lp2;
    Button btn1,btn2,btn3;
    LinearLayout ll1,ll2;
    Intent i;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remainders);
        //<Alternate Location> : Intent works if placed here

        TabHost th=(TabHost)findViewById(R.id.tabhost);
        th.setup();

        TabHost.TabSpec spec=th.newTabSpec("tag1");
        spec.setContent(R.id.tab1);
        spec.setIndicator("REMAINDERS");
        th.addTab(spec);

        spec=th.newTabSpec("tag2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("AUTO REMAINDERS");
        th.addTab(spec);

        spec=th.newTabSpec("tag3");
        spec.setContent(R.id.tab3);
        spec.setIndicator("USER REMAINDERS");
        th.addTab(spec);

        //adding remainder control
        int ht=LayoutParams.WRAP_CONTENT;
        int wt=LayoutParams.FILL_PARENT;
        lp1=new LinearLayout.LayoutParams(wt,ht);
        ll2=(LinearLayout)findViewById(R.id.tab1_ll);

        //Adding Listeners for the buttons in each tab
        //Tab1
        btn1=(Button)findViewById(R.id.tab1_add1);
        btn1.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                btn1.setText("");
                i=new Intent(RemaindersPage.this,weekly.class);
                startActivity(i);
                //AddRemainder();
            }
        });

        //Tab2
        btn2=(Button)findViewById(R.id.tab2_add2);
        btn2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                btn2.setText("");
                //AddRemainder();
            }
        });

        //Tab3
        btn3=(Button)findViewById(R.id.tab3_add3);
        btn3.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                btn3.setText("");
                //AddRemainder();
            }
        });


    }

    //draw a new reminder_control instance
    private void AddRemainder()
    {
        rc1=new com.commsware.pgtracker.RemainderControl(RemaindersPage.this);
        rc1.setId(1000+count);
        ll2.addView(rc1,lp1); 
        rc1.SetText("MESSAGE "+Integer.toString(count),0);
        count++;
    }

    //query for all rows in the table and obtain a cursor
    /*private void fillData()
    {
        cursor=dbHelper.fetchAllReminders();
        startManagingCursor(cursor);
    }

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        if (dbHelper != null) 
        {
            dbHelper.close();
        }
    }*/
}

In the code i am writing i placed an intent to open a new activity inside an onclicklistener but it is not working. Other code in the listener works, also if i place the same intent outside the listener in an "alternate location" it works. So i am not sure what the problem is here. i would really appreciate any help , thanks.

This is the code used in the tab activity that contains the button for which the Listener is written.

public class RemaindersPage extends Activity
{
    com.commsware.pgtracker.RemainderControl rc1;
    int count;
    LinearLayout.LayoutParams lp1,lp2;
    Button btn1,btn2,btn3;
    LinearLayout ll1,ll2;
    Intent i;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remainders);
        //<Alternate Location> : Intent works if placed here

        TabHost th=(TabHost)findViewById(R.id.tabhost);
        th.setup();

        TabHost.TabSpec spec=th.newTabSpec("tag1");
        spec.setContent(R.id.tab1);
        spec.setIndicator("REMAINDERS");
        th.addTab(spec);

        spec=th.newTabSpec("tag2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("AUTO REMAINDERS");
        th.addTab(spec);

        spec=th.newTabSpec("tag3");
        spec.setContent(R.id.tab3);
        spec.setIndicator("USER REMAINDERS");
        th.addTab(spec);

        //adding remainder control
        int ht=LayoutParams.WRAP_CONTENT;
        int wt=LayoutParams.FILL_PARENT;
        lp1=new LinearLayout.LayoutParams(wt,ht);
        ll2=(LinearLayout)findViewById(R.id.tab1_ll);

        //Adding Listeners for the buttons in each tab
        //Tab1
        btn1=(Button)findViewById(R.id.tab1_add1);
        btn1.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                btn1.setText("");
                i=new Intent(RemaindersPage.this,weekly.class);
                startActivity(i);
                //AddRemainder();
            }
        });

        //Tab2
        btn2=(Button)findViewById(R.id.tab2_add2);
        btn2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                btn2.setText("");
                //AddRemainder();
            }
        });

        //Tab3
        btn3=(Button)findViewById(R.id.tab3_add3);
        btn3.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                btn3.setText("");
                //AddRemainder();
            }
        });


    }

    //draw a new reminder_control instance
    private void AddRemainder()
    {
        rc1=new com.commsware.pgtracker.RemainderControl(RemaindersPage.this);
        rc1.setId(1000+count);
        ll2.addView(rc1,lp1); 
        rc1.SetText("MESSAGE "+Integer.toString(count),0);
        count++;
    }

    //query for all rows in the table and obtain a cursor
    /*private void fillData()
    {
        cursor=dbHelper.fetchAllReminders();
        startManagingCursor(cursor);
    }

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        if (dbHelper != null) 
        {
            dbHelper.close();
        }
    }*/
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

无悔心 2024-12-09 04:49:28

尝试这样

  button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent myIntent = new Intent(v.getContext(),
                NextActivity.class);
        startActivityForResult(myIntent, 0);
         finish();


    }
});

Try like this

  button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent myIntent = new Intent(v.getContext(),
                NextActivity.class);
        startActivityForResult(myIntent, 0);
         finish();


    }
});
ˇ宁静的妩媚 2024-12-09 04:49:28

更改:i=new Intent(that,weekly.class);

更改为:i=new Intent(RemaindersPage.this, weekly.class);

并重试

Change: i=new Intent(that,weekly.class);

To: i=new Intent(RemaindersPage.this, weekly.class);

and try again

乖乖兔^ω^ 2024-12-09 04:49:28

在侦听器内部,上下文丢失...您必须使用 v.getContext 检索它

Inside the listener the context get lost... You have to retrieve it using v.getContext

终遇你 2024-12-09 04:49:28

这里的“那个”是什么??使用 RemaindersPage.this 代替“那个”。你将会获得成功。

What is "that" here??.Use RemaindersPage.this in place of "that". You will get success.

余生共白头 2024-12-09 04:49:28

我发现错误实际上是在xml中。
为其编写侦听器的按钮在屏幕上永远不可见。
一旦我修复了就可以了。
完全是我的错。

I found that the error is actually in the xml.
The button for which the listener was written was never visible on the screen.
Once i fixed that it was ok.
Totally my bad.

只想待在家 2024-12-09 04:49:28

在这里,您使用 this 作为 Intent() 构造函数的第一个参数。但由于您位于 OnClickListener() 范围内,因此您将无法访问 this。所以基本上你需要获取当前活动的上下文。这可以通过在视图上调用 getContext() 来完成(onClickListener() 的参数)。

    `v.getContext()`

Here, you are using this as a first parameter of Intent() constructor. But since you are in the scope of OnClickListener() you will not have the access of this. So basically you need to get the context of the current activity. This can be done by calling getContext() on the view (the parameter of onClickListener().

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