2016-03-05 31 views
-1

我想一個網頁添加到我的應用程序,它包含一個按鈕 但其在第一次面臨這樣的錯誤錯誤巴頓findViewById()

/src/com/me/my/bk/BkFragment.java:57: error: cannot find symbol 
     button = (Button) findViewById(R.id.button); 

這是9個錯誤,我解決了他們,現在我有這個錯誤在這裏是我的代碼:

package com.me.my.bk; 

import com.me.my.R; 
import android.app.Fragment; 
import android.os.Bundle; 
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.annotation.SuppressLint; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 
import java.io.IOException; 

public class BkFragment extends Fragment { 
    public static final String TAG = BkFragment.class.getSimpleName(); 

    public static BkFragment newInstance() { 
     return new BkFragment(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_bk, container, false); 
    } 

    private Button button; 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new OnClickListener() { 
      @SuppressLint("SdCardPath") 
      @Override 
      public void onClick(View arg0) { 
       Process p=null; 
       try { 
        p = new ProcessBuilder() 
        .command("/sdcard/test.sh") 
        .start(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally { 
        if(p!=null) p.destroy(); 
       } 
      } 
     }); 
    } 
} 

任何解決這個錯誤im面臨? 由於事先

+0

如果任何回覆對您有幫助,您應該接受答案,http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work。 ;)我看到你提出的另一個問題,你不接受任何答案,也許你在這裏是新的 – JMR

回答

0

好做的onCreateView並不代表一切。此代碼應該可以工作。

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); 

    } 
+1

這就是確切的解決方案感謝您的幫助:))) –

0

你應該在的onCreate

View a = inflater.inflate(R.layout.fragment_bk, container, false); 

listView = (ListView) a.findViewById(R.id.button); 
+0

現在我得到這個/ home/nikan/DualBootPatcher/Android_GUI/src/com/github/chenxiaolong/dualbootpatcher/bk /BkFragment.java:57:錯誤:無法找到符號 setContentView(R.layout.fragment_bk); –

+0

我編輯我的答案,因爲我沒有看到這是一個片段 – JMR