2016-03-28 190 views
0

我嘗試了幾乎所有的東西,但它似乎並沒有工作,我一直無法解析底部按鈕的findviewById。按鈕聲明具有相同的錯誤,但我正確地修正了這些錯誤,底部的按鈕聲明似乎沒有相同的行爲方式。新的android,幫助?findviewbyid錯誤無法解析

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class ConnectFragment extends Fragment { 

public String sign = ""; 
public String total= ""; 
public Double tempDouble, tempDouble2; 

public ConnectFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_connect, container, false); 

    return rootView; 

    // Buttons for Felony 
    Button button1 =(Button)rootView.findViewById(R.id.button1); 
    Button button2 =(Button)rootView.findViewById(R.id.button2); 
    Button button3 =(Button)rootView.findViewById(R.id.button3); 
    Button button4 =(Button)rootView.findViewById(R.id.button4); 
    Button button5 =(Button)rootView.findViewById(R.id.button5); 
    //Buttons for misdemeanors 
    Button button6 =(Button)rootView.findViewById(R.id.button7); 
    Button button7 =(Button)rootView.findViewById(R.id.button8); 
    Button button8 =(Button)rootView.findViewById(R.id.button6); 
    //Buttons for Infraction 
    Button button9 =(Button)rootView.findViewById(R.id.button9); 
    //clear, +, = 
    Button button10=(Button)rootView.findViewById(R.id.button10); 
    Button button11 =(Button)rootView.findViewById(R.id.button11); 
    Button button12 =(Button)rootView.findViewById(R.id.button12); 




    // button 1 event handler 
    button1.setOnClickListener(
      //button 1 interface 
      new Button.OnClickListener() 
      { 
       //Button 1 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        output.append("100 years"); 
       } 
      } 
    ); 
    // button 2 event handler 
    button2.setOnClickListener(
      //button 2 interface 
      new Button.OnClickListener() 
      { 
       //Button 2 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        output.append("25 years"); 
       } 
      } 
    ); 
    // button 3 event handler 
    button3.setOnClickListener(
      //button 3 interface 
      new Button.OnClickListener() 
      { 
       //Button 3 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        output.append("10 years"); 
       } 
      } 
    ); 
    // button 4 event handler 
    button4.setOnClickListener(
      //button 4 interface 
      new Button.OnClickListener() 
      { 
       //Button 4 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        output.append("5 years"); 
       } 
      } 
    ); 
    // button 5 event handler 
    button5.setOnClickListener(
      //button 5 interface 
      new Button.OnClickListener() 
      { 
       //Button 5 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        output.append("1 years"); 
       } 
      } 
    ); 
    // button 6 event handler 
    button6.setOnClickListener(
      //button 1 interface 
      new Button.OnClickListener() 
      { 
       //Button 6 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        output.append("1 years"); 
       } 
      } 
    ); 
    // button 7 event handler 
    button7.setOnClickListener(
      //button 7 interface 
      new Button.OnClickListener() 
      { 
       //Button 7 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        output.append("0.5 years"); 
       } 
      } 
    ); 
    // button 8 event handler 
    button8.setOnClickListener(
      //button 8 interface 
      new Button.OnClickListener() 
      { 
       //Button 8 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        output.append("0.25 years"); 
       } 
      } 
    ); 
    // button 9 event handler 
    button9.setOnClickListener(
      //button 9 interface 
      new Button.OnClickListener() 
      { 
       //Button 9 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        output.append("0.013 years"); 
       } 
      } 
    ); 
    // button 10 event handler 
    button10.setOnClickListener(
      //button 10 interface 
      new Button.OnClickListener() 
      { 
       //Button 10 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        output.setText(""); 
       } 
      } 
    ); 
    // button 12 event handler 
    button12.setOnClickListener(
      //button 12 interface 
      new Button.OnClickListener() 
      { 
       //Button 12 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        tempDouble = Double.parseDouble(output.getText().toString()); 
        output.setText(""); 
        sign = "+"; 
       } 
      } 
    ); 

    // button 11 event handler 
    button11.setOnClickListener(
      //button 11 interface 
      new Button.OnClickListener() 
      { 
       //Button 11 callback method 
       public void onClick(View v) 
       { 
        TextView output = (TextView)findViewById(R.id.editText); 
        tempDouble2 = Double.parseDouble(output.getText().toString()); 
        if (sign=="+") 
        { 
         output.setText(Double.toString(tempDouble+tempDouble2)); 
         sign=""; 
        } 
       } 
      } 
    ); 
} 
} 
} 

} 
+0

將'return'語句移動到'onCreateView()'方法的末尾。 –

+0

@MikeM。仍然收到錯誤http://puu.sh/nWva6/b6cd23a672.png –

+0

哦,那些。假設'TextView'在'Fragment'的佈局中,在getView()'上調用'findViewById()'。也就是說,'TextView output =(TextView)getView()。findViewById(R.id.editText);'。或者你可以製作'rootView'' final',然後調用它。另外,你可以考慮製作你的'Button'和'TextView'變量類成員,並在'onCreateView()'中初​​始化它們。 –

回答

1

你正在寫一個Fragment不具有法findViewById。您需要使用:

TextView output = (TextView) rootView.findViewById(R.id.edittext); 

還將您的return rootView語句移動到方法結尾。

+0

是否這樣? http://puu.sh/nWwsn/da7feb15e9.png我仍然得到錯誤tho –

+0

是的。還要在setClickHandlers之後放置return語句。並使用相同的方式在其中獲取'TextView'。 – Abdullah

0

不是一個真正的答案,但更好的方式來擺脫所有看起來醜陋的樣板代碼是使用依賴注入。使用它的一個好的框架非常簡單容易,就是butterknife。你的代碼會更清潔,你不會有findById和所有這些點擊事件。所有這些都由緩衝刀處理。