I searched many threads but couldn't understand the problem in my code. I am trying to add setOnClickListener functionality on a button from a fragment. It gives me
nullpointer exception at allNews.setOnClickListener(new View.OnClickListener().
Upon debugging I understand that the variable allNews is null.
Below is my layout code.
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id = "#+id/b_all"
android:layout_width="70dp"
android:layout_height="70dp"
android:gravity="center"
android:text="All \n news"
android:textColor="#FFF"
android:textSize="15sp"
android:background="#drawable/button_bg_round"
android:padding="5dp"
android:textAllCaps="false"
/>
And here is my java code:
public class CategoriesFragment extends Fragment {
public Button allNews;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_news_menu, container, false);
getActivity().setTitle("Play");
allNews = (Button) rootView.findViewById(R.id.b_all);
return rootView;
}
public void onViewCreated(View view, #android.support.annotation.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
allNews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(getActivity(), Newsmain.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
I am unable to understand what am I missing. There is a scrollview inside the same xml. Should we handle such layouts/fields seperately ?
Any help in this regard is highly appreciated.
Thanks
Satya
SOLVED: I renamed the xml with activity_news_category and then the variable is not null anymore. Prev the xml was named as activity_news_menu. Thnx for all who attempted to help.
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_news_menu, container, false);
getActivity().setTitle("Play");
allNews = (Button) rootView.findViewById(R.id.b_all);
allNews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(getActivity(), Newsmain.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
return rootView;
}
Use set onclicklistener in OncreateView method.Hope this helps
public void onViewCreated(View view, #android.support.annotation.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
allNews = (Button) view.findViewById(R.id.b_all);
allNews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(getActivity(), Newsmain.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
UPDATE:
instead of rootView it should be view
Add allNews = (Button) rootView.findViewById(R.id.b_all); in onViewCreated() and remove it from onCreateView()
1) Close the LinearLayout properly.
2) Remove the space between the android:id = "#+id/b_all", so you have:
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/b_all"
android:layout_width="70dp"
android:layout_height="70dp"
android:gravity="center"
android:text="All \n news"
android:textColor="#FFF"
android:textSize="15sp"
android:background="#drawable/button_bg_round"
android:padding="5dp"
android:textAllCaps="false"
/>
</LinearLayout>
Then rebuild the project.
Related
I have created an activity PlayActivity that starts a new thread and a game loop. When I have this class as my launcher activity (MainActivity) it works perfectly. But I obviously do not want my app to start with the game loop! In my MainActivity I have different tabs as fragments, in my Home tab, I have a button. I want the game to start with the button clicked. Here is my PlayActivity:
public class PLayActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
Constants.SCREEN_WIDTH = dm.widthPixels;
Constants.SCREEN_HEIGHT = dm.heightPixels;
try {
InputStream is = getAssets().open("test 3.xml");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(new PlayPanel(this, is));
} catch(IOException e) {e.printStackTrace();}
}
}
Main Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View inflatedView = getLayoutInflater().inflate(R.layout.home, null);
basic = (Button) inflatedView.findViewById(R.id.basic);
intermediate = (Button) inflatedView.findViewById((R.id.intermidiate));
basic.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg) {
// Start NewActivity.class
Intent basicIntent = new Intent(MainActivity.this, PLayActivity.class);
startActivity(basicIntent);
}
});
Home.java:
public class Home extends Fragment {
Button basic;
Button intermediate;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.home, container, false);
return view;
}
}
and home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:text="Basic"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="0dp"
android:id="#+id/basic"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="Intermidiate"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="0dp"
android:id="#+id/intermidiate"
android:layout_below="#+id/basic"
android:layout_centerHorizontal="true" />
<Button
android:text="Advanced"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="0dp"
android:id="#+id/advanced"
android:layout_below="#+id/intermidiate"
android:layout_centerHorizontal="true" />
</RelativeLayout>
When I click the button nothing happens! What should I do?
I created a fragment which contains a FrameLayout containing a few buttons:
<FrameLayout android:id="#+id/menu_view"
android:layout_width="490dp"
android:layout_height="490dp"
android:layout_gravity="bottom|end"
android:layout_marginRight="-200dp"
android:layout_marginEnd="-200dp"
android:layout_marginBottom="-200dp"
android:background="#drawable/menu_view_bg">
<Button android:id="#+id/menu_btn1"
android:layout_width="76dp"
android:layout_height="76dp"
android:paddingTop="15dp"
android:paddingBottom="10dp"
android:layout_marginLeft="185dp"
android:layout_marginStart="185dp"
android:layout_marginTop="107dp"
android:background="#drawable/menu_btn_bg"
android:drawableTop="#drawable/menu_item0"
android:textColor="#color/colorPrimaryDark"
android:textAllCaps="false"
android:text="#string/menu1"/>
<Button android:id="#+id/menu_btn2"
android:layout_width="76dp"
android:layout_height="76dp"
android:paddingTop="15dp"
android:paddingBottom="10dp"
android:layout_marginLeft="200dp"
android:layout_marginStart="200dp"
android:layout_marginTop="15dp"
android:background="#drawable/menu_btn_bg"
android:drawableTop="#drawable/menu_item1"
android:textColor="#color/colorPrimaryDark"
android:textAllCaps="false"
android:text="#string/menu2"/>
...
</FrameLayout>
In my fragment class, I scale the menu on a click on a Button outside of FrameLayout but when I try to click on buttons inside it, it won't work. If I don't scale the view at start, it works.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FrameLayout menuView = (FrameLayout) getView().findViewById(R.id.menu_view);
menuView.setScaleX(0.1f);
menuView.setScaleY(0.1f);
Button btn = (Button) getView().findViewById(R.id.menu_main_btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggleMenu();
}
});
Button hotelBtn = (Button) getView().findViewById(R.id.menu_btn2);
hotelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("fef", "onClick: dodkoz");
}
});
}
Specifically, if I comment these two lines, it will work (I set the onclick in my fragment class, not in XML):
menuView.setScaleX(0.1f);
menuView.setScaleY(0.1f);
I guess this has something to do with hit area but I can't find what exactly. I'm fairly new to Android dev so I haven't come up with a workaround. Thanks!
you can use this.
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.yourframelayout, container, false);
Button btn = (Button) view.findViewById(R.id.menu_main_btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// toggleMenu();
}
});
Button hotelBtn = (Button) view.findViewById(R.id.menu_btn2);
hotelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("fef", "onClick: dodkoz");
}
});
}
the following is my java file for one of the fragments that am using to have multiple image buttons which open multiple weebpages.
please help.
treat me as a noob.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.menu8_layout, container, false);
return rootview;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button bt1 = (Button) findViewById(R.id.btn_click_login);
btn_login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http://google.com"));
startActivity(myWebLink);
});
}
}
here is the related xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bt1"
android:clickable="true"
android:autoLink="web"
android:cursorVisible="true"
android:linksClickable="true"
android:onClick="goToSo"
android:text="some" />
I have an Activity with a button like this:
<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"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="onButtonClickListener"
android:text="Show Fragment" />
</RelativeLayout>
And my onButtonClickListener method from xml is(here I create the DialogFragment and set onClickListener for a clear button from dialog):
public void onButtonClickListener(View view) {
FragmentManager fm = getFragmentManager();
MyDialogFragment dialogFragment = new MyDialogFragment ();
dialogFragment.show(fm, "Sample Fragment");
final EditText messageText = (EditText) findViewById(R.id.editText);
Button clearButton = (Button) findViewById(R.id.button_clear);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
messageText.setText(null);
}
});
}
My problem is that when DialogFragment shows it throws NullPointerException:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.cristi.dialogfragmenttest2.MainActivity.onButtonClickListener(MainActivity.java:51)
At this line of code: clearButton.setOnClickListener(new View.OnClickListener() {
My xml for DialogFragment is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:maxLength="200" />
<Button
android:id="#+id/button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
What could be the problem with the setOnClickListener? Why does it says that my button from DialogFragment is null?
Any reference or advice is welcome! Thanks in advance!
Edit:
-MyDialogFragment code:
public class MyDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
getDialog().setTitle("Enter message");
return rootView;
}
You must have refernece to the root view where is your button button_clear, then call rootView.findViewById(...) to get the button reference...
Solution:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
getDialog().setTitle("Enter message");
Button clearButton = (Button) rootView.findViewById(R.id.button_clear);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
messageText.setText(null);
}
});
return rootView;
}
I encounter some issue with getting the reference to a Texview inside a Fragment, and I really have no idea why.
Here is my layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvLogout"
android:layout_below="#+id/tvSettings"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
And here is the Java Class
public class TabPersonnalSpace extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_personnal_space, container, false);
TextView tvSettings = (TextView)rootView.findViewById(R.id.tvSettings);
Log.d("Debug", tvSettings.getText().toString());
tvSettings.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Settings", Toast.LENGTH_SHORT);
}
});
return rootView;
}
}
Thing is : I get the text printed to the logcat when swipe to the Fragment, and nothing happened when I click on the TextView.
Does anyone have a clue for me?
You forget .show() to display Toast
Toast.makeText(getActivity(), "Settings", Toast.LENGTH_SHORT).show();
implement onViewCreated like this :
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//instantiate tvSettings
TextView tvSettings = (TextView)rootView.findViewById(R.id.tvSettings);
}