【发布时间】:2011-04-28 17:05:26
【问题描述】:
为了操作数据库,我创建了一个扩展 SQLiteHelper 的类 DBAdapter()...这个类有这个构造函数:
public DBAdapter(Context context) {
super(context, DATABASE_NAME, null, 1);
this.myContext = context;
}
现在在包的任何活动中都可以正常工作:
DBAdapter db=new DBAdapter(this);
我要做的是创建一个新线程并从该线程访问数据库。
问题是当我尝试做的时候
db=new DBAdapter(this); 在那个类里面告诉我“错误的上下文”!!!
我应该在 DBAdapter() 类中为我的新类创建一个新的构造函数吗???在 DBAdapter() 中是否允许两个构造函数??
这就是我的班级的样子:
public class WorkerRunnable implements Runnable{
public WorkerRunnable(Socket clientSocket){
this.clientSocket=clientSocket;
}
public void run(){
try{
Scanner is =new Scanner(new DataInputStream(this.clientSocket.getInputStream()));
while (is.hasNext()) {
longitude = is.next();
Log.d("Longitudine:",longitude);
latitude=is.next();
Log.d("Latitudine:",latitude);
}
is.close();
clientSocket.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
我的构造函数应该是什么样子?应该在这个类的run()方法中调用吗?谢谢!
public class Server1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server1);
DBAdapter db=new DBAdapter(this);
MultiThreadedServer server = new MultiThreadedServer(6000);
new Thread(server).start();
}
}
如何将 Server1 活动传递给线程????
这里是用于 WorkerRunnable 的 MultiThreadedServer:
公共类 MultiThreadedServer 实现 Runnable {
protected int serverPort=6000;
public static String SERVERIP="10.0.2.15";
protected ServerSocket serverSocket=null;
protected boolean isStopped=false;
protected Thread runningThread=null;
public static int clientconnection = 0;
public MultiThreadedServer(int port){
this.serverPort=port;
}
public void run(){
synchronized(this){
this.runningThread = Thread.currentThread();
}
openServerSocket();
while(!isStopped()){
Socket clientSocket=null;
try{
System.out.println("Serverul asteapta clienti spre conectare");
clientSocket=this.serverSocket.accept();
clientconnection++;
System.out.println("Serverul a acceptat clientul cu numarul:"+clientconnection) ;
//Log.d("Server:","s-a acceptat un nou client");
}
catch(IOException e){
if(isStopped()) {
System.out.println("Server Stopped.") ;
return;
}
throw new RuntimeException(
"Error accepting client connection", e);
}
new Thread(new WorkerRunnable(clientSocket)).start();
}
System.out.println("Server Stopped.") ;
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop(){
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
InetSocketAddress serverAddr =new InetSocketAddress(SERVERIP,serverPort);
serverSocket = new ServerSocket();
serverSocket.bind(serverAddr);
Log.d("Server:","bind-ul a fost realizat!");
} catch (IOException e) {
throw new RuntimeException("Cannot open port 6000", e);
}
}
}
【问题讨论】:
-
您提供的 Runnable 不会调用 DBAdapter 构造函数,因此很难弄清楚您的上下文来自何处。似乎您正在线程中执行 new DBAdapter(this),但由于线程不是上下文,因此这不可能。
-
我想我没有说得很清楚!!!在我的 Runnable 类中,我应该调用应该在 DBAdapter() 类中定义的构造函数。提供的代码适用于应该调用构造函数(我还不知道它的样子!)
-
DBAdapter(this)...我说我在上下文中执行此操作...扩展了 Activity 的类...我不知道如何在线程中执行此操作.....提供的Runnable在线程中!
标签: android multithreading constructor