Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击时,MClick不工作_Java_Android_Android Listview_Onitemclicklistener - Fatal编程技术网

Java 单击时,MClick不工作

Java 单击时,MClick不工作,java,android,android-listview,onitemclicklistener,Java,Android,Android Listview,Onitemclicklistener,我错过了一些非常重要的东西,但我不太清楚是什么。有人能帮忙吗。这可能是我错过了一些非常愚蠢的事情,但我不能开始我的恶作剧 一旦创建 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); actu_ip = intent.getStringExtra(IPEntry.ACT

我错过了一些非常重要的东西,但我不太清楚是什么。有人能帮忙吗。这可能是我错过了一些非常愚蠢的事情,但我不能开始我的恶作剧

一旦创建

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
    setContentView(R.layout.act_ipcontrol);

    mainListView = (ListView) findViewById( R.id.mainListView );
    String[] options = new String[] { "All in to 1", "Spare"};  

    ArrayList<String> optionsList = new ArrayList<String>();
    optionsList.addAll( Arrays.asList(options) );
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, optionsList);
    mainListView.setAdapter( listAdapter ); 

    try {
        Toast.makeText(IPControl.this, "Please wait...Connecting...", Toast.LENGTH_SHORT).show();
        new AsyncAction().execute();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

private class AsyncAction extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... args) { 
        try {
            InetAddress serverAddr = InetAddress.getByName(actu_ip);
            socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
            OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
            BufferedWriter bw = new BufferedWriter(osw);
            out = new PrintWriter(bw, true); 
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (! in .ready());
            readBuffer();
            out.println("root\r\n");
            while (! in .ready());
            readBuffer();
            out.println("root\r\n");
            while (! in .ready());
            readBuffer();
            out.println("[verbose,off\r\n");
            while (! in .ready());
            String msg = "";
            while ( in .ready()) {
                msg = msg + (char) in .read();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;//returns what you want to pass to the onPostExecute()
    }

    protected void onPostExecute(String result) {

        Toast.makeText(IPControl.this, "Connected", Toast.LENGTH_SHORT).show();

        //results the data returned from doInbackground

        IPControl.this.data = result;

    }
}

private String readBuffer() throws IOException {
    String msg = "";

    while(in.ready()) {
        msg = msg + (char)in.read();
    }
    //System.out.print(msg);
    if(msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
    else if(msg.indexOf("SCX_COM> ") != -1) return msg.substring(0, msg.indexOf("SCX_COM> "));
    else return msg;
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent=getIntent();
actu_ip=intent.getStringExtra(IPEntry.ACTUALSMARTIP);
setContentView(R.layout.act\u ipcontrol);
mainListView=(ListView)findViewById(R.id.mainListView);
字符串[]选项=新字符串[]{“全部输入到1”,“备用”};
ArrayList选项列表=新建ArrayList();
optionsList.addAll(Arrays.asList(options));
listAdapter=new ArrayAdapter(this,R.layout.simplerow,optionsList);
mainListView.setAdapter(listAdapter);
试一试{
Toast.makeText(IPControl.this,“请稍候…连接…”),Toast.LENGTH\u SHORT.show();
新建AsyncAction().execute();
}捕获(例外e){
e、 printStackTrace();
}
}
私有类AsyncAction扩展了AsyncTask{
受保护的字符串doInBackground(字符串…args){
试一试{
inetAddressServerAddr=InetAddress.getByName(actu_ip);
套接字=新套接字(serverAddr,重定向的\u SERVERPORT);
OutputStreamWriter osw=新的OutputStreamWriter(socket.getOutputStream());
BufferedWriter bw=新的BufferedWriter(osw);
out=新的PrintWriter(bw,真);
in=新的BufferedReader(新的InputStreamReader(socket.getInputStream());
而(!in.ready());
readBuffer();
out.println(“root\r\n”);
而(!in.ready());
readBuffer();
out.println(“root\r\n”);
而(!in.ready());
readBuffer();
out.println(“[verbose,off\r\n”);
而(!in.ready());
字符串msg=“”;
while(在.ready()中){
msg=msg+(char)in.read();
}
}捕获(未知后异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
return null;//返回要传递给onPostExecute()的内容
}
受保护的void onPostExecute(字符串结果){
Toast.makeText(IPControl.this,“Connected”,Toast.LENGTH_SHORT.show();
//结果从doInbackground返回的数据
IPControl.this.data=结果;
}
}
私有字符串readBuffer()引发IOException{
字符串msg=“”;
while(在.ready()中){
msg=msg+(char)in.read();
}
//系统输出打印(msg);
if(msg.indexOf(“SNX_COM>”)!=-1)返回msg.substring(0,msg.indexOf(“SNX_COM>”);
else if(msg.indexOf(“SCX_COM>”)!=-1)返回msg.substring(0,msg.indexOf(“SCX_COM>”);
否则返回味精;
}
}
我想发起的

        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
            long arg3) {

        try {
            new AsyncAction1().execute();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private class AsyncAction1 extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... args) { 
            try {
                out.println("[c,l#,i1,o*\r\n");
                //System.out.print("root\r\n");
                while(! in .ready());


            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;//returns what you want to pass to the onPostExecute()
        }

        protected void onPostExecute(String result) {

            //results the data returned from doInbackground
            Toast.makeText(IPControl.this, "Command Sent", Toast.LENGTH_SHORT).show();

            IPControl.this.data = result;

        }
    }
public void onItemClick(适配器视图arg0、视图arg1、内部位置、,
长arg3){
试一试{
新建AsyncAction1().execute();
}捕获(例外e){
e、 printStackTrace();
}
}
私有类AsyncAction1扩展了AsyncTask{
受保护的字符串doInBackground(字符串…args){
试一试{
out.println(“[c,l,i1,o*\r\n”);
//System.out.print(“root\r\n”);
而(!in.ready());
}捕获(未知后异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
return null;//返回要传递给onPostExecute()的内容
}
受保护的void onPostExecute(字符串结果){
//结果从doInbackground返回的数据
Toast.makeText(IPControl.this,“命令已发送”,Toast.LENGTH_SHORT.show();
IPControl.this.data=结果;
}
}

我在您的代码中没有看到listview的setOnItemClickListener方法。您实现了吗? 试着跟随

mainListView.setAdapter( listAdapter ); 
mainListView.setOnItemClickListener(new OnItemClickListener(){
   public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {

      try {
        new AsyncAction1().execute();
      }catch(Exception e) {
        e.printStackTrace();
   }
});
mainListView.setAdapter(listAdapter);
mainListView.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
试一试{
新建AsyncAction1().execute();
}捕获(例外e){
e、 printStackTrace();
}
});

谢谢你的帮助,我解决了我的问题

我只是没有按正确的顺序做事

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
    setContentView(R.layout.act_ipcontrol);

    mainListView = (ListView) findViewById( R.id.mainListView );
    final String[] options = new String[] { "All in to 1", "Spare"};  

    ArrayList<String> optionsList = new ArrayList<String>();
    optionsList.addAll( Arrays.asList(options) );
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, optionsList);
    mainListView.setAdapter( listAdapter ); 
    mainListView.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
            try {
                    if(pos == 0) {
                        AsyncAction1 a = new AsyncAction1();
                        a.setCmd("[c,l#,i1,o*\r\n");
                        a.execute();
                    }
                } catch(Exception e) {
                    e.printStackTrace();
                }
        }
    });
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent=getIntent();
actu_ip=intent.getStringExtra(IPEntry.ACTUALSMARTIP);
setContentView(R.layout.act\u ipcontrol);
mainListView=(ListView)findViewById(R.id.mainListView);
最终字符串[]选项=新字符串[]{“全部输入到1”,“备用”};
ArrayList选项列表=新建ArrayList();
optionsList.addAll(Arrays.asList(options));
listAdapter=new ArrayAdapter(this,R.layout.simplerow,optionsList);
mainListView.setAdapter(listAdapter);
mainListView.setOnItemClickListener(新的OnItemClickListener(){
公共链接(AdapterView arg0、视图arg1、内部位置、长arg3){
试一试{
如果(位置==0){
AsyncAction1 a=新的AsyncAction1();
a、 setCmd(“[c,l,i1,o*\r\n”);
a、 执行();
}
}捕获(例外e){
e、 printStackTrace();
}
}
});
然后

private class AsyncAction1 extends AsyncTask<String, Void, String> {
    String cmd;

    public void setCmd(String c) {
        cmd = c;
    }

    protected String doInBackground(String... args) {
        try {
            out.println(cmd);
            //System.out.print("root\r\n");
            while(! in .ready());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;//returns what you want to pass to the onPostExecute()
    }

    protected void onPostExecute(String result) {

        //results the data returned from doInbackground
        Toast.makeText(IPControl.this, "Command Sent", Toast.LENGTH_SHORT).show();

        IPControl.this.data = result;
    }
}
} 
私有类AsyncAction1扩展了AsyncTask{
字符串cmd;
公共void setCmd(字符串c){
cmd=c;
}
受保护的字符串doInBackground(字符串…args){
试一试{
out.println(cmd);
//System.out.print(“root\r\n”);
而(!in.ready());
}捕获(未知后异常e){
e、 printStackTrace();