Intent在onClickListener中不起作用?

问题描述 投票:2回答:6

在我写的代码中,我放置了一个意图在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();
        }
    }*/
}
android buttonclick
6个回答
12
投票

试试这样吧

  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();


    }
});

1
投票

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

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

然后再试一次


1
投票

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


0
投票

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


0
投票

我发现错误实际上是在xml中。在屏幕上看不到为其写入监听器的按钮。一旦我修复了它就可以了。完全是我的坏。


0
投票

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

    `v.getContext()`
© www.soinside.com 2019 - 2024. All rights reserved.