OnClickListener on a null object reference - android

Hello can anybody help me to handle this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at de.kwietzorek.fulcrumwebview.MainActivity$AddServerFragment.onCreateView(MainActivity.java:141)
141 is at btn_back.setOnClickListener(new View.OnClickListener() {
Fragment: AddServerFragement
public class AddServerFragment extends Fragment
implements View.OnClickListener {
#Override
public void onClick(View view) {
}
public AddServerFragment(){
}
Button btn_back, btn_add;
EditText server_ip, server_name;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_ip, container, false);
server_ip = (EditText) findViewById(R.id.edit_server_address);
server_name = (EditText) findViewById(R.id.edit_server_name);
btn_back = (Button) findViewById(R.id.btn_back);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String new_server_ip = null, new_server_name = null;
ArrayList<String> server_name_list = new ArrayList<String>();
ArrayList<String> server_ip_list = new ArrayList<String>();
new_server_ip = server_ip.getText().toString();
server_ip_list.add(new_server_ip);
new_server_name = server_name.getText().toString();
server_name_list.add(new_server_name);
}
});
return view;
}
}

Replace
btn_back= (Button) findViewById(R.id.btn_back);
with
btn_back = (Button) view.findViewById(R.id.btn_back);
Do the same for all the other layout elements you wanna initiate.
The reason is: since you inflating a layout file in your Fragment, you have to refer all UI elements to THAT file, that you called "view" here:
View view = inflater.inflate(R.layout.add_ip, container, false);
So the istruction "findViewById" has to be refered to that View view.

You need to find the view inside the view you inflated:
btn_back = (Button) view.findViewById(R.id.btn_back);

Related

How to open activity from fragment

I am just learning programming and faced with such an error. I need to open activity ruki from home fragment. But when I run the emulator, my application crashes
What am I doing wrong
ImageButton ruki;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, null);
final ImageButton imageButton = (ImageButton) view. findViewById(R.id.ruki);
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.ruki:
Intent intent = new Intent( HomeFragment.this.getActivity(), ruki_activity.class);
startActivity(intent);
break;
}
}
};
ruki.setOnClickListener(onClickListener);
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
you are never initiating ImageButton ruki;, you are obtainging reference to final ImageButton imageButton, which isn't even used. ruki.setOnClickListener(onClickListener); will cause NullPointerException (ALWAYS post exception stacktrace)
instead of creating new variable
final ImageButton imageButton = (ImageButton) view. findViewById(R.id.ruki);
just attach result of findViewById to declared one
ruki = (ImageButton) view. findViewById(R.id.ruki);
public void onClick(View view) {
Intent intent= new Intent(getActivity(),UsingBackKey.class);
startActivity(intent);
}

imagebutton inside fragment nullPoint Exception

I am putting ImageView inside Fragment. I have other question also but cannot find useful.
this is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_fragment_contect,container,false);
ImageButton imageButton = (ImageButton)getView().findViewById( R.id.ib);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity( new Intent( getActivity(), Enquiry.class ) );
}
});
return v;
}
You need to do like this in getview() you getting null pointer exception use view.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_fragment_contect,container,false);
ImageButton imageButton = v.findViewById(R.id.ib);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity( new Intent( getActivity(), Enquiry.class ) );
}
});
return v;
}
You got NullPointException because of using getview():
ImageButton imageButton = (ImageButton)getView().findViewById( R.id.ib);
Solution: use v instead of getView()
ImageButton imageButton = (ImageButton) v.findViewById( R.id.ib);
Android Fragment Referance

How to show Relative Layout if it's not empty

I have a Relative Layout and the visisbility is set to gone if the Recycler View is empty. My problem is that it doesn't return when I add things into the recycler view
This is my Class
#Override
public View inOnCreateView(View root, #Nullable ViewGroup container, Bundle savedInstancesState) {
View view = mRoot;
addButton = view.findViewById(R.id.addItemsButton);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent addButton = new Intent(getActivity(), AddItemsActivity1.class);
startActivity(addButton);
}
});
mModelList = new ArrayList<>();
mRecyclerView = view.findViewById(R.id.recycler_view1);
mAdapter1 = new RecyclerViewAdapter(mModelList);
LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(manager);
editText = view.findViewById(R.id.editTxt);
btn = view.findViewById(R.id.btn1234);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mModelList.add(new Model(editText.getText().toString()));
mAdapter1.notifyDataSetChanged();
}
});
mRecyclerView.setAdapter(mAdapter1);
relativeLayout = view.findViewById(R.id.Dairy_RelativeLayout);
if(mAdapter1.getItemCount() == 0){
relativeLayout.setVisibility(View.GONE);
}
return root;
}
I also know it's not my adapters fault because when I set it to View.Invisible I can see that the view gets bigger when items are in it, since its set to wrap content.

Button click throws nullpointerexception in fragment

Im getting an error when I press the button in my fragment view. The button is rendered but when I click it I get the following error:
09-23 18:29:01.242 9332-9332/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at foit.startingpoint.nl.androiddrawer.LocatiesFragment.onCreateView(LocatiesFragment.java:28)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button button = (Button) getView().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(LocatiesFragment.this.getActivity(), SecondActivity.class);
startActivity(myIntent); }
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_locaties, container, false);
}
your initializing is incorrect, you must do as following code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_locaties, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(),SecondActivity.class);
startActivity(myIntent);
}
});
return view;
}
as onCreateView not finished getView is null in your code so you must create one view and use that.
You cannot simply getView() from nowhere when finding button id. First layout is inflated.
View view = inflater.inflate(R.layout.fragment_locaties, container, false);
Button button = (Button) view.findViewById(R.id.button);
...
return view;

openFileOutput is undefined for the type

I'm following a tutorial where in his openFileOutput there's a suggestion to surround it with try catch. But in my code, it only suggest to create method openFileOutput(String, int). I'm using a fragment btw. Here's my code:
public class Tab1 extends Fragment implements OnClickListener {
TextView text_fname, text_lname;
EditText edit_fname, edit_lname;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab1, container, false);
text_fname = (TextView) v.findViewById(R.id.text1);
text_lname = (TextView) v.findViewById(R.id.text2);
edit_fname = (EditText) v.findViewById(R.id.edit1);
edit_lname = (EditText) v.findViewById(R.id.edit2);
Button button = (Button) v.findViewById(R.id.button1);
button.setOnClickListener(this);
return v;
}
#Override
public void onClick(View arg0) {
String fname1 = text_fname.getText().toString();
String fname2 = edit_fname.getText().toString();
String lname1 = text_lname.getText().toString();
String lname2 = edit_lname.getText().toString();
FileOutputStream fos=openFileOutput("test.txt", Context.MODE_PRIVATE);
}
}
Change the line to the following:
FileOutputStream fos = getActivity().openFileOutput("test.txt", Context.MODE_PRIVATE);

Categories

Resources