站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题手机阅读器!帮我解决下,谢谢!(续)

手机阅读器!帮我解决下,谢谢!(续)

摘要: 手机阅读器!帮我解决下,谢谢!(续)


上次的那些很多朋友已经帮助我解决,并让我有了很深刻的了解,现在又出新问题
我想获得文件名称,有2种方法:
1种,在本身类里写个线程获得,这个运用没问题。
2种,我另外写了个线程的类,然后在主类中调用该线程类来接收文件名称,出现错误了。
为什么第2种就不行?
我已经想了10几个小时了,还是没搞出来,也不可能是死锁,因为本类属于一个线程,我取名字属于一个线程,2个线程不可能操作到相同数据,所以我认为不应该死锁,但到底是什么问题,希望大家帮忙解决下,拜托了拜托了。
希望大家能把第2种方法实现,并且给我说说我写的那个地方错误?错误原因?解决方法?谢谢

第一种方法的代码

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;


public class file_name extends MIDlet implements CommandListener,Runnable {
private FileConnection f_conn = null;
private String tmp=null;
private Display display;
private Form f_f;
private Command list_exit=new Command("exit",Command.EXIT,1);
private Command list_start=new Command("start",Command.SCREEN,1);
private Thread t_file;

public file_name() {
t_file=new Thread(this);
display=Display.getDisplay(this);
f_f=new Form("");
f_f.addCommand(list_exit);
f_f.addCommand(list_start);
f_f.setCommandListener(this);
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

}

protected void pauseApp() {


}

protected void startApp() throws MIDletStateChangeException {
display.setCurrent(f_f);

}
public void commandAction(Command c,Displayable s){
if (c.getLabel().equalsIgnoreCase("Start"))
{
open_flie();
}
}
protected void open_flie()
{





t_file.start();
}
public void run(){

try{
f_conn = (FileConnection)Connector.open("file:///root1/f_text.txt",Connector.READ);
if(f_conn.exists())
{
tmp="";
tmp=f_conn.getName();
f_f.append(tmp);
f_conn.close();
}
else
{

tmp="file is not exists";
f_f.append(tmp);
display.setCurrent(f_f);
f_conn.close();

}
}
catch(Exception e)
{}

}

}


第二种方法的代码
本类


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;



public class file_name extends MIDlet implements CommandListener {
private String tmp=null;
private Display display;
private Form f_f;
private Command list_exit=new Command("exit",Command.EXIT,1);
private Command list_start=new Command("start",Command.SCREEN,1);

public file_name() {
display=Display.getDisplay(this);
f_f=new Form("");
f_f.addCommand(list_exit);
f_f.addCommand(list_start);
f_f.setCommandListener(this);
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

}

protected void pauseApp() {


}

protected void startApp() throws MIDletStateChangeException {
display.setCurrent(f_f);

}
public void commandAction(Command c,Displayable s){
if (c.getLabel().equalsIgnoreCase("Start"))
{
open_flie();
}
}
protected void open_flie()
{
ddclass thread = new ddclass();
new Thread(thread).start();
while (!thread.hasDone()) {
try {
Thread.sleep(3000);
}
catch (InterruptedException ex) {
}
}
if (thread.hasDone()){;
tmp=thread.get_name();

}
f_f.append(tmp);
display.setCurrent(f_f);
}


}
线程类


import javax.microedition.io.file.*;
import javax.microedition.io.*;



public class ddclass implements Runnable {
private String name;
private FileConnection f_conn = null;
private boolean hasDone = false;



public void run(){

try{

f_conn = (FileConnection)Connector.open("file:///root1/qq.txt");
if(f_conn.exists())
{
name=f_conn.getName();
f_conn.close();
}
else
{

name="file is not exists";
f_conn.close();

}
hasDone = true;
}

catch(Exception e)
{
System.out.println(e);
}


}
public boolean hasDone() {
return hasDone;
}
public String get_name()
{
return name;
}

}








去你的油箱看看


我看了,FC的API我也正在看
我希望你能针对我的第二种方法,把它错误的地方改好了能运用通过。
然后发回给我,指出我错误在哪里?发生这种错误的问题?

我想只有在不断错误中,认识错误,改掉错误才能提高


你的方法其实是没有办法改的,因为整个的设计思路有问题,要改的话你只能改到跟原来一点不相关,你应该先学一些基本的程序结构,遵循一种规范,而不能靠自己想的来.
你对线程的理解还不好,线程不是你想象中那么用的,所以我建议你放弃自己原来的代码,重新写一分,你可以模仿那个例子的结构来


为什么我的改不了?
设计思路有问题,只能说设计出来的程序运行效率低吧,而不是不能运行
我相信,肯定能改得运用,请你仔细看看,帮忙一下
你给的那个我看了,非常感谢你,我对结构也清晰了很多
但我这个人比较倔强,既然那个地方出现错误,我相信肯定是我线程那里用得不对
那不对就该改,错误出在什么地方,我要清楚,清楚了才不会避免下次再出现同样错误,你说对不?


路过,友情up...


如果把open_flie()里的代码放在一个新的线程里就可以了
还有
while (!thread.hasDone()) {
try {
Thread.sleep(3000);
}
catch (InterruptedException ex) {
}
sleep可以去掉
就是while里面什么都不做


确实 把open_file()里的代码放进一个新线程就好了

我以前的那个是线程出错?错在哪里?为什么要新放条线程就好了

请zitengxin指教下,谢谢



j2medev里面已经回复你了


↑返回目录
前一篇: 编译PRC的问题
后一篇: 在J2ME中 声音播放问题?