开发者

Issue's containing, "onClick" and a getIntExtra

I am having issues getting this to work, here is what I am trying to get to: an onClickListener that will get the state of the radio and return a toast. But I am messing up somewhere. I am having an issue with the intent, anyone able to shed some light on this?the intent is not able to be resolved. (Yes, I obviously have imported it.) Here is what I have:

public class MainActivity extends Activity {
protected Intent intent;
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button check = (Button)findViewById(R.id.Button01);
    check.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v){
            getIntent();
            int state= intent.getIntExtra(TelephonyManager.EXTRA_STATE, -1);
            String msg=null;
                    switch (state) {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                Toast.make开发者_StackOverflow中文版Text(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                break;
        }
    }

    });
}

}


You don't store the value returned by getIntent(). This is better :

public void onClick(View v){
        intent = getIntent();
        int state= intent.getIntExtra(TelephonyManager.EXTRA_STATE, -1);


Don't forget to call show()
manual

example

Toast toast = Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT);
toast.show();

Another thing is indeed in this line

int state=intent.getIntExtra(TelephonyManager.EXTRA_STATE, -1);

What is "intent" here? Is it declared somewhere in the class?

You say in the comment that you're getting a forceclose in this line. Is it a nullpointer? Do you need to call getIntent() somewhere maybe? Is that 'intent' initialized?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜