android 开发中两个类之间的context传递问题..
代码如下.packagecom.helloworld;importandroid.app.Activity;importandroid.os.Bundle;importa...
代码如下.
package com.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.helloworld.*;
import android.widget.*;
public class HelloWorld11Activity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=new TextView(this);
Button button=(Button)findViewById(R.id.Button1);
button.setOnClickListener(new MyClick(getApplicationContext()));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openDialog();
}
public void openDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Hello");
builder.setMessage("Hello World \n");
builder.setNegativeButton("OK",null);
builder.show();
}
}
class MyClick extends Activity implements OnClickListener {
Context context1;
public MyClick (Context context) {
this.context1 = context;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
openDialog();
}
public void openDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(context1);
builder.setTitle("Hello");
builder.setMessage("Hello World \n");
builder.setNegativeButton("OK",null);
builder.show();
}
}
请高手支教啊 展开
package com.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.helloworld.*;
import android.widget.*;
public class HelloWorld11Activity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=new TextView(this);
Button button=(Button)findViewById(R.id.Button1);
button.setOnClickListener(new MyClick(getApplicationContext()));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openDialog();
}
public void openDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Hello");
builder.setMessage("Hello World \n");
builder.setNegativeButton("OK",null);
builder.show();
}
}
class MyClick extends Activity implements OnClickListener {
Context context1;
public MyClick (Context context) {
this.context1 = context;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
openDialog();
}
public void openDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(context1);
builder.setTitle("Hello");
builder.setMessage("Hello World \n");
builder.setNegativeButton("OK",null);
builder.show();
}
}
请高手支教啊 展开
4个回答
展开全部
getApplicationContext():在这里空指针 得到的是NUll 所以 不行:这通常“只能”用于当你需要的上下文的生命周期是独立于当前上下文的时候 。所以
button.setOnClickListener(new MyClick(getApplicationContext()));不能用,改成button.setOnClickListener(new MyClick(this));或者button.setOnClickListener(new MyClick(HelloWorld11Activity.this));
button.setOnClickListener(new MyClick(getApplicationContext()));不能用,改成button.setOnClickListener(new MyClick(this));或者button.setOnClickListener(new MyClick(HelloWorld11Activity.this));
展开全部
楼主先表达什么问题..是MyClick里面调用不了ConText么??
如果是这样试试用HelloWorld11Activity.this 毕竟在同一个Activity呀..
如果是这样试试用HelloWorld11Activity.this 毕竟在同一个Activity呀..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
同意楼上;而且MyClick类有必要继承Activity吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询