Android - ping,无法解析方法" findViewById(int)"

时间:2015-08-18 20:19:49

标签: java android android-layout android-fragments

我是Android编程的绝对初学者,我有以下代码:

<span class="post-tag">
	  aasdfas
	  <span class="delete-tag" title="remove this tag"></span>
</span>



<span class="post-tag">
	  basdfasdf
	  <span class="delete-tag" title="remove this tag"></span>
</span>

<span class="post-tag">
	  basdfasdf
	  <span class="delete-tag" title="remove this tag"></span>
</span>


<br/>
<br/>
<br/>

<img src="https://cdn0.iconfinder.com/data/icons/gcons-2/24/laptop1-16.png" />

我几乎没有问题:

1)为什么我有错误&#34;无法解决方法&#34; findViewById(int)&#34;&#34;在部分

public class First_Fragment extends Fragment {

    View myView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.first_layout, container, false);
        return myView;


    }



    public boolean executeCommand(){
        System.out.println("executeCommand");
        Runtime runtime = Runtime.getRuntime();
        try
        {
            Process  mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
            int mExitValue = mIpAddrProcess.waitFor();
            System.out.println(" mExitValue " + mExitValue);
            if(mExitValue==0){
                return true;
            }else{
                return false;
            }

            TextView tv = (TextView)findViewById(R.id.textView2);
            tv.setText(mExitValue);

        }
        catch (InterruptedException ignore)
        {
            ignore.printStackTrace();
            System.out.println(" Exception:"+ignore);
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.out.println(" Exception:"+e);
        }
        return false;


    }

}

将其更改为&#34; ... view.findViewById(...&#34;不起作用(&#34;无法解析方法&#34;查看(整数)&#34;&#34 ;

2)当我想要那个&#34;标签&#34;作为&#34; ping&#34;

的文本输出

3)如何使用&#34; ping&#34;仅导出该部分如果服务器没有响应(超时或等),它会将该标签的文本更改为其他内容等#34;服务器关闭&#34;)

如果你帮助我,我会很高兴,我只想再说一遍,我是绝对的初学者,所以请不要怪我。谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

tv = (TextView) myView.findViewById(R.id.textView2);

此外,片段需要定义正在使用其组件的视图。这里myView是布局膨胀的视图,因此我们需要通过以下方式定义该片段使用myView的textview:

var myfunc = {
    copy: function(message){
        window.setTimeout(function(){
           console.log(message);
        },1000);
        return this;
    }
};
myfunc.copy('hello').copy('world'); 
// wait a second then log:
// hello
// wait until first function completion (1000ms), wait a second then log:
// world