I have a problem when creating a simple RecyclerView
When I import what is necessary to use it, I always get the same error message.
In the main class i have the following imports:
import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
and in then adapter class, the followings:
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
I want to emphasize that I have imported import android.view.View; by the need of the ViewHolder (View view = mInflater.inflate ...)
Well, I always get an error with the following message:
Something that I find very curious is also that in the ViewHolder of the adapter, with a TextView, the same thing happens to me:
In my build.gradle file, these are my imports:
I am using Android Studio 3.1.2
I appreciate the help.
Thanks in advance.
You need to cast your view like
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
Your problem will be solved.
But if you compile your app with API 26, you don't even need to cast, and your existing code will work. :) like
In older versions:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
In AS3.0 with sdk 26:
RecyclerView recyclerView = findViewById(R.id.rv);
Related
I have came across a very strange problem, i am using Pager in android to navigate between different screens through fragments in android. also i am using another fragment class to create Layouts automatically in a grid,
for fragment 1, where i have used the pager and pageradapter i am using these imports
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
for the other fragment which i am using to populate images in a non linear Grid, i am using this import
import android.app.Fragment;
The issue is:
By using this import i am unable to add my fragments into the pageradapter. it keeps on giving me error.
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(MyFragment.newInstance("Tv Shows"));
fList.add(MyFragment.newInstance("Movies"));
fList.add(MyFragment.newInstance("Music Videos"));
return fList;
}
also i cannot initialize my pageadapter
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
it gives me error on this line to.
My Question is:
what is difference between the two imports
import android.support.v4.app.Fragment;
and
import android.app.Fragment;
is there any solution to the problem, ?
`import android.app.Fragment`
requires minimum API level your app requires is 11 or higher. to use in in application with API less than 11 you need to use support library. when using support library you should use
import android.support.v4.app.Fragment;
I am trying to implement an activity which converts units. There are five fields which I want to be able to convert between on the fly. I can't simply use TextWatcher because when I change a field then that changes all the other fields and cause a circular call which crashes.
I looked at OnFocusChangeListener but that only works when the focus changes whereas I want the update to occur on the fly.
I then thought I could combine the two so I have my TextWatcher and inside that I put this
public void afterTextChanged(Editable s)
{
if (convert_kg.hasfocus())
{
if (convert_kg.getText().toString().matches("")) sums=0.0;
else sums=Double.parseDouble(convert_kg.getText().toString());
answer=(int)Math.round(sums*2.205);
convert_lbs.setText(String.format("%d", answer));
}
}
but I am getting an error of The method hasfocus() is undefined for the type EditText
These are my imports:
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.text.Editable;
import android.text.TextWatcher;
As far as I can tell from http://developer.android.com/reference/android/widget/EditText.html it should be supported? I'm sure this is simple but any ideas?
EDIT: In case anyone was wondering all the fields are EditText
convert_kg=(EditText)findViewById(R.id.convert_kg);
See the comments. isFocused() was the method I needed to use.
I have an application in which i want to add validation, but when i run only validation page it works fine but when i rut it with this page it gives me error.
I think i am doing mistake where i am adding an imagebutton by which i call the validation page which name is propertysearch.
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
Well the stacktrace says there's a NullPointerException at line 32 in your ViewPagerStyle1Activity. Look what call is at that line and you should know the culprit.
You are trying to find imageButton1 in your layout main, but it is actually located in your layout layout_one. The fix now depends on what you are actually plan to do with your fragment. You could just set the fragment layout as the layout for your ViewPagerStyle1Activity by replacing setContentView(R.layout.main); with setContentView(R.layout.layout_one); in its' onCreate method.
Update: maybe you should read a bit first about Android Activities, Layouts and Fragments. This is very basic stuff and you won't get far without learning it in and out.
Of course you can't find the imageButton1, if you hard-code findViewById to return null in your fragment.
private ImageButton findViewById(int imagebutton1) {
// TODO Auto-generated method stub
return null;
}
But I get the sense you really don't know what you are actually trying to do.
2Hi guys I'm trying to build an app using a horizontal pager and the support package for Android. I've made this exact code compile in another project but the last line of the second code example is not letting me compile. Eclipse is saying Cannot instantiate the type PagerAdapter
My imports
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
My code
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Tab1.class.getName()));
fragments.add(Fragment.instantiate(this, Tab2.class.getName()));
fragments.add(Fragment.instantiate(this, Tab3.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
Do you think there is something wrong with my imports or project set up - let me know if you need more information. This code has worked in other projects.
Thanks
PagerAdapter is an abstract class – you cannot instantiate it. You have to create a new class that inherits from PagerAdapter and use that instead.
Just create a new class that inherits from PagerAdapter and use it instead.
NB: Don't forget to remove the initial import
"import android.support.v4.view.PagerAdapter;"
and import the newly created class.
I'm getting "cannot be resolved to a type" in my code. I'm looking for solution but all solutions are not working.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class Vortex extends Activity {
private static final String LOG_TAG = Vortex.class.getSimpleName();
private VortexView _vortexView; //there is the problem
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_vortexView = new VortexView(this);
setContentView(_vortexView);
}
}
The problem is with VortexView in this code. Eclipse tell me that VortexView cannot be resolved to a type.
Any solution?
Do you have VortexView class in your project, if so, ctrl+shift+O will resolve the issue. Otherwise add class/jar to classpaht and do ctrl+shift+O (organize imports).
VortexView is your custom view? If there exists VortexView.class
Probably, you have made either of the following two mistakes.
You have not extended a view named VortexView at all
You have extended it correctly, but you haven't made it available to the current package
When you are doing such things, it is better to define custom views inside the same package. Or even though you are defining it somewhere else, make it avilable where it is used.
import yourPackage.VortexView;
Hope this helps.