0

I've been looking at many examples of getting the pointers to the displays in the onCreate function but every time I do this I get null returns from my findViewById functions but if I do it elsewhere it works just fine. What am I missing to get this to work right? Thanks for any help I must be missing something

EditText messageField;
AutoCompleteTextView numberField;
EditText countField;
TextView messageDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    sms = SmsManager.getDefault();
    preferences = PreferenceManager.getDefaultSharedPreferences(this);

    messageField = (EditText) findViewById(R.id.message);
    numberField = (AutoCompleteTextView) findViewById(R.id.number);
    countField = (EditText) findViewById(R.id.count);
    messageDisplay = (TextView) findViewById(R.id.display_message);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
        .add(R.id.container, new PlaceholderFragment()).commit();
    }


}

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#000000"
tools:context="com.example.last.MainActivity$PlaceholderFragment" >


<AutoCompleteTextView
    android:id="@+id/number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/send"
    android:layout_marginTop="49dp"
    android:textColor="@android:color/white"
    android:inputType="numberSigned"
    android:selectAllOnFocus="true"
    android:ems="10"
    android:text="@string/phone_number" >

    <requestFocus />
</AutoCompleteTextView>

<Button
    android:id="@+id/send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/message"
    android:layout_marginRight="25dp"
    android:layout_marginTop="70dp"
    android:onClick="sendMessage"
    android:textColor="@android:color/white"
    android:text="@string/send_button" />

<EditText
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/send"
    android:layout_below="@+id/number"
    android:layout_marginTop="61dp"
    android:ems="10"
    android:inputType="textMultiLine"
    android:textColor="@android:color/white"
    android:selectAllOnFocus="true"
    android:text="@string/default_message" />

<EditText
    android:id="@+id/count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/message"
    android:layout_marginTop="32dp"
    android:ems="4"
    android:maxEms="4"
    android:inputType="numberSigned"
    android:textColor="@android:color/white"
    android:text="@string/default_count" />

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/message"
    android:layout_below="@+id/send" />

<TextView
    android:id="@+id/display_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/progressBar1"
    android:layout_below="@+id/progressBar1"
    android:layout_marginTop="45dp"
    android:layout_toLeftOf="@+id/send"
    android:textColor="@android:color/white"
    android:text="@string/default_display_message" />

<TextView
    android:id="@+id/char_count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/message"
    android:layout_alignBottom="@+id/message"
    android:layout_alignParentRight="true"
    android:text="18/160" />

<android.gesture.GestureOverlayView
    android:id="@+id/easter_egg_gesture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/display_message" >

</android.gesture.GestureOverlayView>

<Button
    android:id="@+id/eeb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/easter_egg_gesture"
    android:layout_alignParentLeft="true"
    android:background="#000000"
    android:onClick="easterEgg" />

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.joebot.MainActivity"
tools:ignore="MergeRootFrame" />
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
user2967314
  • 13
  • 1
  • 1
  • 3
  • Make sure your all Views that you reference is coming from `activity_main.xml` file. – M D May 30 '14 at 05:54
  • I see, I will go look up how I do that – user2967314 May 30 '14 at 05:59
  • @user2967314 go to this [post](http://stackoverflow.com/questions/23869019/nullpointerexception-thrown-when-trying-to-findviewbyid/23869046#23869046) – M D May 30 '14 at 06:01

3 Answers3

0

From your code, looks like your components like EdiText and TextView are residing in Fragment_main.xml but you are referencing them in Activity_main.xml.

Change your initialization of components to correct layout.

Ramakishna Balla
  • 1,020
  • 1
  • 8
  • 12
0

The problem is, the following four lines of code:

messageField = (EditText) findViewById(R.id.message);
numberField = (AutoCompleteTextView) findViewById(R.id.number);
countField = (EditText) findViewById(R.id.count);
messageDisplay = (TextView) findViewById(R.id.display_message);

These lines should come AFTER your fragment transaction:

if (savedInstanceState == null) {
    getFragmentManager().beginTransaction()
    .add(R.id.container, new PlaceholderFragment()).commit();
    getFragmentManager().executePendingTransactions();
}

messageField = (EditText) findViewById(R.id.message);
numberField = (AutoCompleteTextView) findViewById(R.id.number);
countField = (EditText) findViewById(R.id.count);
messageDisplay = (TextView) findViewById(R.id.display_message);

This should work.

Note also that I've added the executePendingTransactions() method to your code (it is required in this case). However, I'd prefer to handle those Views in the Fragment (unless there's a good reason to use them in the Activity itself). Remove these lines from the onCreate() method of your Activity and add them to the onViewCreated() method of your PlaceholderFragment :

@Override
public void onViewCreated(View view, Bundle savedInstanceState){

    messageField = (EditText) view.findViewById(R.id.message);
    numberField = (AutoCompleteTextView) view.findViewById(R.id.number);
    countField = (EditText) view.findViewById(R.id.count);
    messageDisplay = (TextView) view.findViewById(R.id.display_message);

}

Whatever you would like to do with those Views can be done in this method. Hope this helps ... :)

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
0

You are trying to reference Views in your Fragment's layout from your Activity. This is not possible as the Views reside in the Fragment's layout and not the Activity's one which is where it tries to look. You should define these Views in the Fragment that they are in and then expose them through methods if you desire access from your Activity.

You should however not make that decision lightly as it is recommended to keep Fragment details in your Fragments and only expose what you need to outside of that. Such as results or values instead of whole Views.

public MyFragment extends Fragment implements View.OnClickListener
{
    private Button myButton;
    private EditText myEditText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_input_submit, container, false);
        myButton = (Button) view.findViewById(R.id.myButton);
        myEditText = (EditText) view.findViewById(R.id.myEditText);
        myButton.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v)
    {
        // keep the Fragment interaction in the Fragment
        switch (v.getId())
        {
            case R.id.myButton:
                Log.v("example fragment", "myButton was pressed");
                break;
            default:
                break;
        }
    }

    public EditText getMyEditText()
    {
        // you could get the EditText to use in your Activity
        return this.myEditText;
    }

    public String getEditTextContents()
    {
        // or you could just allow access the data
        return this.myEditText.getText().toString();
    }
}

To make use of this you will need to keep a reference to your Fragment in your Activity:

public MyActivity extends FragmentActivity
{
    private MyFragment myFragment;

    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main);

        myFragment = new MyFragment();
        getFragmentManager().beginTransaction()
            .add(R.id.container, myFragment).commit();
    }
    ...
}

Then to get access to the Fragment's information you would call either:

EditText myEditText = myFragment.getMyEditText();
    or
String editTextContents = myFragment.getEditTextContents();

depending on your choice.

You can also communicate from the Fragment to the Activity using Interfaces. See this previous answer I made to a related question here for details on that and more examples of this.

Community
  • 1
  • 1
indivisible
  • 4,892
  • 4
  • 31
  • 50