R.id returns false - android

I have just started playing with Android using eclipse. I have been stalled on this one issue for quite a while now, and I have no idea why it doesn't work.
I have started a new app with as little coding done as possible just to see if the issue still occurs. Basically when I am typing 'R.id.' it will come up with the correct list of possible ids for me to access but when I actually try to run them it returns false.
The current one I'm testing has a very simple xml, where one of the ids is 'text'. So when I type R.id it prompts me with options, one of them is text. So i write R.id.text expecting it to return the id. Also when findViewById(R.id.text) it crashes the app with a null pointer exception on that line.
fragment_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/base"
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"
tools:context="com.reecemerrett.historical.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
MainActivity.java:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
new AlertDialog.Builder(this)
.setTitle("ID")
.setMessage(R.id.text)
.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
This code outputs false. I have tried it with many different ids and even with R.id.container.
I also tried removing the whole fragment style and calling fragment_main.xml directly with setContentView().
Any help is much appreciated.

The TextView belongs to the Fragment Layout ie fragment_main.xml.
So change to
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView tv = (TextView) rootView.findViewByid(R.id.text);
new AlertDialog.Builder(getActivity())
.setTitle("ID")
.setMessage(tv.getText().toString())
.show();
return rootView;
}
Or
As you already tried
I also tried removing the whole fragment style and calling fragment_main.xmldirectly with setContentView().
This will work. Make sure you get rid of Fragment related code like
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
TextView tv = (TextView)findViewByid(R.id.text);
new AlertDialog.Builder(this)
.setTitle("ID")
.setMessage(tv.getText().toString())
.show();
}
}

Try this..
Remove AlertDialog.Builder from MainActivity put that code in PlaceholderFragment like below
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
new AlertDialog.Builder(getActivity())
.setTitle("ID")
.setMessage(((TextView) rootView.findViewById(R.id.text)).getText().toString().trim())
.show();
return rootView;
}
}

I faced similar problem before. Since your view is inside your fragment findViewByID will not find it for you. Why? I don't know!! But it shouldn't be a problem because you can find this view inside your fragment's onCreateView callback. Assuming your view is a textView the code will look like this
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content_view, container, false);
TextView viewText = (TextView) view.findViewById(R.id.text);
return view;
}
Normally this would solve the problem because Activity doesn't need to know about views inside fragments. Fragments should handle their affair themselves and talk to activity only when it's necessary. However your activity still can access the this data using Fragment To Activity - Activity To Fragment Communication Technics. I will not explain them since there is a very good documentation on developer.android.com

Related

No PlaceholderFragment class in MainActivity.java

I am trying to learn android programming by google's android programming course (android studio) in udacity.
The problem is I do not have "PlaceholderFragment" class in my MainActivity.java.
I only have MainActivity class in this file.
Could anyone help me please?
Edit:
my MainActivity.java is:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I experienced the same problem when using Udacity course "Developing Android Apps: Android Fundamentals". The Udacity videos are old > 9 months and since android studios is always updating, the newer version of android studio does not provide an inner class for the fragment anymore. Instead android studio generates a separate class containing a fragment. But, no problem.
Go to the tab of Lesson 1: "Using the Sunshine Github Repository" and open the link: Udacity Sunshine repository
Download the branch : "1.01_hello_world" and import the contents into android studios. You will have the "PlaceholderFragment" class within the MainActivity.java and then you can continue the lessons at Udacity
You need to read a good webpage. I recommend reading and studying code from Google's # Fragments.
Code snippets from the webpage:
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Note: ExampleFragment could be your "PlaceholderFragment".
Just delete your code succeeding your package name in the MainActivity.java file and replace it with this one below.
import android.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
I got the same problem, right after I put the "ForecastFragment" class into it's own file.
What solved the problem for me was to go build - clean project. After doing that, it built just fine.
Perhaps there are some old build files that look for placeholderFragment?
I had the same problem, but I tried to figure out how to work with the new structure. I put the code inside MainActivityFragment.java, inside the method onCreateView, since this is the new approach. It worked for me.
Take a look of my final code:
public class MainActivityFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container);
ArrayList<String> weekForecast = new ArrayList<>();
weekForecast.add("Today - Sunny - 88/63");
weekForecast.add("Tomorrow - Foggy - 70/40");
weekForecast.add("Weds - Cloudy - 72/63");
weekForecast.add("Thurs - Asteroids - 75/65");
weekForecast.add("Fri - Heavy Rain - 65/56");
weekForecast.add("Sat - HELP TRAPPED IN WEATHERSTATION - 60/51");
weekForecast.add("Sun - Sunny - 80/68");
mForecastAdapter = new ArrayAdapter<>(getActivity(),
R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast);
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
//return inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}

Transferring code from ActivityBased UI to fragment based UI

I am getting a null pointer exception at line 39 due to the text view being created in fragment.xml and its code being added in main activity class. What shall I do to the highlighted piece of code to make it work in the same way, but in the PlaceHolderFragment class?
package com.example.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;
import android.os.Build;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
} */
getSupportActionBar().show();
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
***TextView textView = (TextView)findViewById(R.id.textView1);
textView.setTextSize(40);
textView.setText(message);***
// Set the text view as the activity layout
setContentView(R.layout.fragment_display_message);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
The inflation of the textview should be inside the fragment class if it was added in the fragment layout(same as activity). So move the textview inside the onCreateView() method of the fragment. It should look something like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_calendar, container,
false);
TextView textView = (TextView)view.findViewById(R.id.textView1);//make this a class variable
textView.setTextSize(40);//put this method in onStart()
textView.setText(message);//put this method in onStart()
return view;
}
hi i think you should write
// Create the text view
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setTextSize(40);
textView.setText(message);
below
setContentView(R.layout.fragment_display_message);

App crashes when adding onClickListener Android [duplicate]

This question already has answers here:
setOnClickListener crashs my android app
(3 answers)
Closed 8 years ago.
I don't know why but I can't seem to figure out what is wrong with my code. As soon as I add an onclickListener the app crashes. I really need help. Also I looked at other threads but I can't seem to find out what I'm doing wrong. Thanks
The code is below:
package com.example.medrec;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
login = (Button)findViewById(R.id.w_login);
//Crashes when I add this line.
login.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
My 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.example.medrec.MainActivity"
tools:ignore="MergeRootFrame" />
My 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"
tools:context="com.example.medrec.MainActivity$PlaceholderFragment" >
<EditText
android:id="#+id/w_number_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
android:text="#string/w_number" />
<Button
android:id="#+id/w_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/w_number_edit_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="#string/w_login_text" />
try below code:-
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
login = (Button)rootView.findViewById(R.id.w_login);
//Crashes when I add this line.
login.setOnClickListener(this);
return rootView;
}
}
I'm assumging it's crashing because your login is null, the reason it's null because w_login doesn't exist in your activity layout. Try this (rootView from your fragment),
login = (Button) rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);
Your app crashed it's because your button w_login belong to your fragment_main.xml and you have to set activity_main.xml layout to your Activity.
At code
login = (Button) rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);
You should reference this Button in your Fragment onCreateView(......)
assign on click listener in onCreateView
login = (Button) rootView.findViewById(R.id.w_login);
//Crashes when I add this line.
login.setOnClickListener(this);
Your app crashes showing NullPointerException while searching the Button in your activity_main.xml but it doesn't find there. Rather you should add the button code in your PlaceholderFragment fragment.
Delete the following line from your MainActivity
login = (Button)findViewById(R.id.w_login);
//Crashes when I add this line.
login.setOnClickListener(this);
Add the following to your PlaceholderFragment
public static class PlaceholderFragment extends Fragment implements OnClickListener{
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button login = (Button)rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);
return rootView;
}
}

Android findViewById is returning Null in Eclipse generated project

I created a new Android project in Eclipse with one Blank Activity. In the fragment, I added a TextView called textHello. All I want to do for this test is to modify textHello's text value. But I'm my findViewById is giving me null.
Here is the fragment code:
<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"
tools:context="com.example.gcuitest.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Here is the MainActivity.java
package com.example.gcuitest;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
private TextView helloText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
//getFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment()).commit();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, new PlaceholderFragment());
fragmentTransaction.commit();
}
helloText = (TextView) findViewById(R.id.textHello);
helloText.setText("Gerard");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Why is findViewById giving me null? Is it in the right place? I've looked at other code samples, and they place findViewById in onCreate, after setContentView, which is what this does. What did I miss? Thanks.
Why is findViewById giving me null?
Because textHello TextView is inside PlaceholderFragment layout instead of Activity which is activity_main.
if you want to access TextView from Fragment layout then use onCreateView as :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
helloText = (TextView)view. findViewById(R.id.textHello);
helloText.setText("Gerard");
return view;
}

Android app error "Unfortunately the app is stopped"

I am using Android studio for android development.I am creating onclick button activity.I am getting no errors but when I click the button on the app it shows "Unfortunately the app has stopped."
Below is my code for fragment_main.xml and MainActivity.java.
Please guide.
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/texthaiku"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/editText"
android:onClick="OnLoveButtonClicked"/>
<TextView
android:text="#string/haiku"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_below="#+id/button"
android:visibility="invisible"
android:layout_alignParentLeft="true"
android:layout_marginTop="33dp" />
</RelativeLayout>
MainActivity.java
package com.AndroidLove;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void onLoveButtonClicked(View view) {
TextView textView =(TextView) findViewById(R.id.textView);
textView.setVisibility(View.VISIBLE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Try this..
It'll give NPE because textView is inside fragment_main.xml then your initilize that textview inside click function that's wrong . use that textView in global
TextView textView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
textView =(TextView) rootView.findViewById(R.id.textView);
return rootView;
}
and
public void onLoveButtonClicked(View view) {
textView.setVisibility(View.VISIBLE);
}
where did you create container, in xml which you have shown i don't see any container layout.
without R.id.container
you have not initialized your TextView on onCreateView()
so add this line on onCreateView()
textView =(TextView) rootView.findViewById(R.id.textView);
EDIT
initialize your button also.
btn = (Button) rootView.findViewById(R.id.button);

Categories

Resources