Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a layout file with the following attributes;

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

Java
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
Java
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
Updated 11-Jun-15 11:05am
v4

1 solution

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900