Click here to Skip to main content
13,731,697 members
Rate this:
 
Please Sign up or sign in to vote.
I have a layout file with the following attributes;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content">
    <ImageView

        android:layout_width="match_parent"

        android:layout_height="64dp"

        android:scaleType="center"

        android:background="#FFFFBB33"

        android:contentDescription="@string/app_name" />
    <EditText

        android:id="@+id/cityDialog"

        android:inputType="textCapWords"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="16dp"

        android:layout_marginLeft="4dp"

        android:layout_marginRight="4dp"

        android:layout_marginBottom="4dp"

        android:hint="@string/city_hint" />
    <EditText

        android:id="@+id/stateDialog"

        android:inputType="textCapWords"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="4dp"

        android:layout_marginLeft="4dp"

        android:layout_marginRight="4dp"

        android:layout_marginBottom="16dp"

        android:fontFamily="sans-serif"

        android:hint="@string/state_hint"/>

    <EditText

        android:id="@+id/countryDialog"

        android:inputType="textCapWords"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="4dp"

        android:layout_marginLeft="4dp"

        android:layout_marginRight="4dp"

        android:layout_marginBottom="16dp"

        android:fontFamily="sans-serif"

        android:hint="@string/country_hint"/>
</LinearLayout>



Then I have a corresponding class with the following declarations;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.R.*;

public class settings extends DialogFragment{

public static final String IMAGE_RESOURCE_ID = "iconResourceID";
public static final String ITEM_NAME = "itemName";

private EditText city = (EditText) findViewById(R.id.cityDialog);
private EditText state;
private EditText country;


public settings(){}

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



But I'm getting a
Cannot resolve method 'findViewById(int)'
and I'm not sure why.


Things I've tried;
- Removing the R file so that the resource can be generated
- Cleaning and rebuilding the entire project
- Renaming the attributes

But nothing seems to be working. What am I missing?
Posted 11-Jun-15 10:23am
Updated 11-Jun-15 11:05am
v4

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You need to first call the getView() function to get the view of the fragment and then you can call the findViewById().

Like this,

private EditText city = (EditText) getView().findViewById(R.id.cityDialog);


Now it should work. Or you can use the getActivity() for the fragment because you cannot directly call findViewById() in a Fragment or DialogFragment and so on. Also, you should check and execute these functions in onActivityCreated() event.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 11 Jun 2015
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100