android studio local variable redundant [duplicate] - java

This question already has answers here:
"Local variable is redundant" using Java
(5 answers)
Closed 2 years ago.
Can someone explain to me why it's giving me "local variable is redundant error"?
package com.example.smite.floater;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.zip.Inflater;
public class creator extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater,#Nullable ViewGroup container,#Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.creator ,container,false);
return v;
}
}
At start it was a different error on the R only
but after I changed the xml this happend

In your method you are:
returning a value and after that creating a variable. It is an error, because this code will never run
you are creating a variable which is not being used
Your code:
public View onCreateView(LayoutInflater inflater,#Nullable ViewGroup container,#Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
// It is after a return statement!
View v = inflater.inflate(R.layout.creator ,container,false);
//You are creating a variable which is not being used
return v;
}
Use:
public View onCreateView(LayoutInflater inflater,#Nullable ViewGroup container,#Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.creator ,container,false);
}

Allocating or creating less objects is a good practice in android. In your case you are allocating a View object which is not being used.
use
return inflater.inflate(R.layout.creator ,container,false);
instead of
View v = inflater.inflate(R.layout.creator ,container,false);
return v;

Related

Tabbed Activity Android Studio [duplicate]

This question already has answers here:
How do I "findViewById" with fragments?
(2 answers)
Closed 4 years ago.
I wanted to know something, I want to make an application on android with Tabbed Activity, I have everything done, but I have a problem, I want to put TextViews in one of the tab and it does not let me instantiate, if I put the
match = (TextView) findViewById (R.id.match);
The program tells me:
Can not resolve method 'findViewById (int)'
How could I do it? Is there any way?
package com.example.jose.firebaseimportante;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class TabbedUsuarioFutbol extends Fragment{
public TabbedUsuarioFutbol() {}
private TextView partido1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.futbol_tabbed_usuario, container, false);
return rootView;
match = (TextView)findViewById(R.id.match);
}
}
Change
match = (TextView)findViewById(R.id.match);
to
match = rootView.findViewById(R.id.match);
and
return rootView;
should be the last statement in your onCreateView method because any statement after a return statement is unreachable statement, i.e. it will never execute.
Your onCreateView method should look like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.futbol_tabbed_usuario, container, false);
match = rootView.findViewById(R.id.match);
return rootView;
}

null point exception when trying to fetch data from openweathermap [duplicate]

This question already has answers here:
getView returning null when fragment has been created from an activity
(6 answers)
Closed 5 years ago.
I am trying to get sunrise and sunset time for my current location from openweathermap api. I am following this tutorial.
I have copied the entire Function.java in the tutorial and trying to use the data to update my ViewPagerAdapter's Fragment.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
TextView cityField = getView().findViewById(R.id.tv_city);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
RecyclerView rv = rootView.findViewById(R.id.rv_recycler_view);
rv.setNestedScrollingEnabled(true);
Function.placeIdTask asyncTask =new Function.placeIdTask(new Function.AsyncResponse() {
public void processFinish(String weather_city, String weather_description, String weather_temperature, String weather_humidity, String weather_pressure, String weather_updatedOn, String weather_iconText, String sun_rise) {
cityField.setText(weather_city);
}
});
asyncTask.execute("25.180000", "89.530000"); // asyncTask.execute("Latitude", "Longitude")
rv.setHasFixedSize(true);
MyAdapter adapter = new MyAdapter(new String[]{"Hello", "World"});
rv.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
return rootView;
}
}
and I have named my target for the city name as tv_city:
<TextView
android:id="#+id/tv_city"
android:layout_toRightOf ="#+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="40sp"
android:text="Hello World"
android:textSize="30sp"
android:layout_marginBottom="5sp"
android:paddingLeft="5sp"
android:gravity="top" >
</TextView>
The problem is, possibly I am making some mistake to fetch the data, as I am getting nul exception error for this line:
TextView cityField = getView().findViewById(R.id.tv_city);
as:
Process: com.example.phocast, PID: 9820 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.phocast.BlankFragment.<init>(BlankFragment.java:17)
WIth my limited knowledge in java, I am unable to solve it.
Kindly help.
Update
actually, writing oncreateview as:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
RecyclerView rv = rootView.findViewById(R.id.rv_recycler_view);
rv.setNestedScrollingEnabled(true);
Weather_OWM.placeIdTask asyncTask =new Weather_OWM.placeIdTask(new Weather_OWM.AsyncResponse() {
public void processFinish(String weather_city, String weather_description, String weather_temperature, String weather_humidity, String weather_pressure, String weather_updatedOn, String weather_iconText, String sun_rise) {
TextView cityField = rootView.findViewById(R.id.tv_city);
cityField.setText(weather_city);
}
});
solves my problem, but i dont know if this is proper/good way to use final. I will be really grateful if someone shows me the way before closing it.
Your cityView line of code is just hanging out there, not in a method or anything. The NullPointer is coming from getView(). You can't find the view before onCreateView occurs and the inflater does its thing. Move it to onCreateView, anytime after View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
You need initialize the textView after:
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
After initialize the recyclerView you can write this:
cityField = rootView.findViewById(R.id.tv_city);

spinner or loader while fragment page get completedly loaded

I have created an android application using Android Fragments as described in Dynamically Prevent Rotation On Android Fragment.
The application is working fine but the problem is that say a particular fragment say Photo Gallery might calls several http calls and might certain time for bringing all the images. So when we click the Photo Gallery it seems stuck for sometime.
How can we show some slider or progress bar like something till the fragment view get loaded completely?
An example like is as shown below:
My PhotoGalleryFragment is given below
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PhotoGalleryFragment extends Fragment {
public PhotoGalleryFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
// some http calls to be triggered
return rootView;
}
}
You can use ProgressDialog, for example:
public class PhotoGalleryFragment extends Fragment {
private ProgressDialog mProgressDialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
// some http calls to be triggered
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mProgressDialog = ProgressDialog.show(activity, "", "Loading...", true, false);
}
}
When you http calls are ended you can dismiss the ProgressDialog with mProgressDialog.dismiss().

Rating Bar Android

Hi I am busy making a program where you get to rate people
I've been playing around with the Rating Bar widget however I am struggling to make it show a rating after selecting the amount of stars. What I'm I doing wrong. I would also like to to know if the android sdk have anything on up and down votes and would it be wise to use images for a rating system.
The reason why some of the code is commented out is because my app won't run when its uncommented but I know its needed to do what I want to do.
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class rating_system_fragment extends Fragment implements OnRatingBarChangeListener{
RatingBar ratingBar;
TextView ratingResult;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater.inflate(R.layout.rating_system_fragment,container,false);
//setContentView(R.layout.activity_main);
return inflater.inflate(R.layout.rating_system_fragment, container, false);
//ratingResult = (TextView) ratingResult.findViewById(R.id.textViewRating);
//((RatingBar) ratingBar.findViewById(R.id.ratingBar))
// .setOnRatingBarChangeListener(this);
}
#Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
final int numStars = ratingBar.getNumStars();
ratingResult.setText(rating + "/" + numStars);
}
}
You have several syntax erros in your commented code. It should look like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.rating_system_fragment,container,false);
ratingResult = (TextView) rootView.findViewById(R.id.textViewRating);
ratingBar = (RatingBar) rootView.findViewById(R.id.ratingBar);
ratingBar.setOnRatingBarChangeListener(this);
return rootView;
}
The key here is that ratingBar and textViewRating are childs of your root view, and should only be accessed when you have aready inflated them.

Fragments: how to change content?

I have a class that manages a fragment .. in a TextView I have to put the phone features, but because by mistake?
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class PageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null);
return view;
}
String model = Build.MODEL;
String product = Build.PRODUCT;
String androidOS = Build.VERSION.RELEASE;
String scheda = model+"\n"+androidOS+" \n "+product+"";
TextView text = (TextView) findViewById(R.id.textView3);
text.setText(scheda);
private TextView findViewById(int textview3) {
// TODO Auto-generated method stub
return null;
}
}
I did not understand a thing. I have three fragment, one of which is called Home, one faq and one about. How do I change the contents of the various "screens"?
Firstly you cannot have operations on objects in the main class body without a helper method in java. I also see when you are creating a reference to you TextView, you need to find the TextView from your inflated view, so the right way to do it would be something like
TextView text = (TextView)view.findViewById(R.id.textview3);
Lastly, when inflating the view, pass the ViewGroup as the parent for that view, and a false for the attachToRoot
You should have something like:
public class PageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page,container,false);
String model = Build.MODEL;
String product = Build.PRODUCT;
String androidOS = Build.VERSION.RELEASE;
String scheda = model+"\n"+androidOS+" \n "+product+"";
TextView text = (TextView) view.findViewById(R.id.textView3);
text.setText(scheda);
return view;
}
}
Hope that helps

Categories