[英]setViewRefs(view) and view.findViewById errors
下面的代碼在按下按鈕時運行一個sh文件
package com.me.me.bk;
import com.me.me.R;
import android.app.Fragment;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View.OnClickListener;
public class BkFragment extends Fragment {
public static final String TAG = BkFragment.class.getSimpleName();
public static BkFragment newInstance() {
return new BkFragment();
}
private Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bk, container, false);
setViewRefs(view);
return view;
}
private void executeScript() {
button = (Button)view.findViewById(R.id.button);
try {
ProcessBuilder pb = new ProcessBuilder(
"/sdcard/test.sh");
Process p = pb.start(); // Start the process.
p.waitFor(); // Wait for the process to finish.
System.out.println("Script executed successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
但是構建APK時出現此錯誤
/home/nikan/mep/Android_GUI/src/com/me/me/bk/BkFragment.java:50:錯誤:找不到符號setViewRefs(view); ^符號:
方法setViewRefs(View)位置:類BkFragment /home/nikan/mep/Android_GUI/src/com/me/me/bk/BkFragment.java:56:錯誤:找不到符號button =(Button)view.findViewById(R。 id.button); ^符號:變量視圖位置:類BkFragment 2錯誤
如您在我的代碼中所見。 我已導入視圖,但出現此錯誤。
視圖對象都不是全局的,因此您不能從其他方法訪問。 其次,沒有按照錯誤日志定義setViewRefs。
試試看
public class BkFragment extends Fragment {
public static final String TAG = BkFragment.class.getSimpleName();
public static BkFragment newInstance() {
return new BkFragment();
}
private Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bk, container, false);
setViewRefs(view);
return view;
}
private void setViewRefs (View view){
button = (Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
executeScript();
}
});
}
private void executeScript() {
try {
ProcessBuilder pb = new ProcessBuilder(
"/sdcard/test.sh");
Process p = pb.start(); // Start the process.
p.waitFor(); // Wait for the process to finish.
System.out.println("Script executed successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.