Can not resolve method 'findViewById(int)'

2019-03-22 22:03发布

问题:

I'm having a trouble with findViewByid but I can't find where the problem is.

Here's my FirstFragment class code:

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.ArrayList;
import java.util.List;


public class FirstFragment extends Fragment {
public static final String TAG = "first";

private WebView mWebView;
ProgressBar progressBar;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    mWebView = (WebView) findViewById(R.id.activity_main_webview);

    progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://www.google.com");
 }

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

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


 }
}

回答1:

You need to do this in onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle   savedInstanceState) {
   View view =  inflater.inflate(R.layout.secondefragment, container, false);
    mWebView = (WebView) view.findViewById(R.id.activity_main_webview);
   progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);

   WebSettings webSettings = mWebView.getSettings();
   webSettings.setJavaScriptEnabled(true);
   mWebView.loadUrl("http://www.google.com");

   return view;


}


回答2:

Fragment doesn't provide thefindViewById() method. This is provided in Activity or View. When implementing a Fragment you don't inflate your views in onCreate() (like you normally do in an Activity.) Instead, you do it in onCreateView() and you need to use the inflated root View to find the ID within the layout you inflated.



回答3:

getActivity().findViewById() works. However, this isn't a good practice because the fragment may be reused in another activity.

The recommended way for this is to define an interface.

  • The interface should contain methods by which the fragment needs to communicate with its parent activity.

    public interface MyInterfcae {
        void showTextView();
    }
    
  • Then your activity implements that Interface.

    public class MyActivity extends Activity implements MyInterfcae {
        @Override
        public void showTextView(){
            findViewById(R.id.textview).setVisibility(View.VISIBLE);
        }
    }
    
  • In that fragment grab a reference to the interface.

    MyInterface mif = (MyInterface) getActivity();
    
  • Call a method.

    mif.showTextView();
    

This way, the fragment and the activity are fully decoupled and every activity which implements that fragment, is able to attach that fragment to itself.



回答4:

I meet the same question in Android studio learning,just because I create the project by Fragment. Choose Blank Activity with no Fragment would solve it.



回答5:

I found solution by changing targetSdkVersion from 23 to 21



回答6:

I had this problem when I downloaded sample project from github. This project had

compileSdkVersion 23
buildToolsVersion "23.0.0"
targetSdkVersion 23

in Activity context and findViewById() were in red color inside onCreate() method. So I updated the above versions to

compileSdkVersion 25
buildToolsVersion "25.0.3"
targetSdkVersion 25

and error got resolved.



回答7:

Add getView() before findViewById() method.

Like this:

getView().findViewById(R.id.potatoes);



回答8:

getActivity().findViewById() This works it removes the red colour of findviewby id and it doesnt say cannot resolve symbol but when i put a colin in the end the whole statement is underlined and it says unreachable statement. barChart =(BarChart)getActivity().findViewById(R.id.barchart); I am also doing this in fragment.



回答9:

This is how you can solve the problem:

toggleButton1=(ToggleButton)findViewById(R.id.toggleButton1);  
toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);