This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I'm initialing imageview at DailyCardView.java which are called by a fragment. But it keeps showing nullPointerException. I really don't know where went wrong... Guys help me><
I tried getRootView() to get view of Fragment so i can code:iv=(ImageView)getRootView().findViewById(R.id.iv);
But it still not work.
**//Fragment**
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment, container, false);
dailyStackLayout = (DailyStackLayout)view.findViewById(R.id.dailyStackLayout);
dailyStackLayout.setDatas(dailyFoodList);
return view;
}
**//DailyStackLayout**
public DailyStackLayout(Context context) {
this(context, null);
}
public DailyStackLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DailyStackLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init() {
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
scaleY = DensityUtil.dip2px(getContext(), BASESCALE_Y_VALUE);
}
public void setDatas(List<DailyFood> dailyFoodList) {
this.dailyFoodList = dailyFoodList;
if (dailyFoodList == null) {
return;
}
for (int i = index; index < i + STACK_SIZE; index++) {
dc = new DailyCardView(getContext());
dc.bind(dailyFoodList.get(index));
}
}
**//DailyCardView**
public DailyCardView(Context context) {
super(context, null);
}
public DailyCardView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public DailyCardView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init(context);
}
public void init(Context context) {
if (!isInEditMode()) {
inflate(context, R.layout.item_daily_card, this);
iv = (ImageView)findViewById(R.id.iv);
}
}
public void bind(DailyFood dailyFood) {
if (dailyFood == null) {return;}
if (!TextUtils.isEmpty(dailyFood.getId())) {
Glide.with(**iv.getContext()**) // **NullPointerException**!
.load(Constant.Daily_URL+dailyFood.getId()+".jpg")
.into(iv);
}
I expect to load the image right but it went wrong at getContext().
dc = new DailyCardView(getContext());
This calls the DailyCardView(Context) constructor that does not call init():
public DailyCardView(Context context) {
super(context, null);
}
The init() call would init iv. And then you call bind() that assumes that iv is initialised. Bang, NPE.
Either change the super in the constructor to this to delegate to another constructor that calls init(), or just add a call to init().
Related
This question already has answers here:
Passing image from one activity another activity
(5 answers)
Closed 5 years ago.
How can I send image res to another Activity - How change Resources to integer?
i create class to pass Image Resources to another activity:
public class MyImageView extends ImageView implements View.OnClickListener {
Context contxt;
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOnClickListener(this);
this.contxt = context;
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
this.contxt = context;
}
public MyImageView(Context context) {
super(context);
setOnClickListener(this);
this.contxt = context;
}
#Override
public void setOnClickListener(OnClickListener l) {
super.setOnClickListener(l);
}
#Override
public void onClick(View v) {
int g = getResourse......................;(this is my peroblem)
Bundle extras = new Bundle();
extras.putInt("imagebitmap", g);
Intent i = new Intent(contxt, zara.knauf.Img.class);
i.putExtras(extras);
contxt.startActivity(i);
}
}
Simply set g to a R.drawable integer value. You don't need to call getResources.
You also don't need to create a Bundle object.
See How to pass integer from one activity to another?
I need to save a number inside an ImageView object so I can access it later in my code. That's why I created a custom class that extends ImageView:
public class SliderView extends ImageView {
private int position;
public SliderView(Context context) {
super(context);
}
public SliderView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SliderView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setViewPosition(int pos){
this.position = pos;
}
public int getPosition(){
return position;
}
}
This is my SimpleDraweeView object in .xml :
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/bottomslider"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
And what I am trying to achieve is this:
final SliderView mediaImage = (SliderView) convertView.findViewById(R.id.bottomslider);
When I try to run the above code, I get this error:
java.lang.ClassCastException:
com.facebook.drawee.view.SimpleDraweeView cannot be cast to
android.custom.SliderView
If I use ImageView instead of SliderView everything works fine, but I don't have the functionality that I need. Is there any workaround?
public class SliderView extends ImageView {
private int position;
public SliderView(Context context) {
super(context);
}
public SliderView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SliderView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SliderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setViewPosition(int pos){
this.position = pos;
}
public int getPosition(){
return position;
}
}
Try this, and let me know if this works
I have a custom view extends RelativeLayout, and I want to add a RecyclerView in it. But the recyclerView can't be shown.
How to solve it? Thanks!
The code:
LineChart.java
public class LineChart extends RelativeLayout {
RecyclerView mRecyclerView;
public LineChart(Context context) {
super(context);
init(context, null);
}
public LineChart(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public LineChart(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
protected void init(Context context, #Nullable AttributeSet attrs) {
setWillNotDraw(false);
initInside();
}
protected void initInside() {
View v = inflate(getContext(), R.layout.line_chart_recyclerview, null);
LineChart.LayoutParams params = new LineChart.LayoutParams(
getWidth() / 2, getHeight());
params.addRule(LineChart.ALIGN_PARENT_RIGHT);
addView(v, params);
mRecyclerView = (RecyclerView) findViewById(R.id.lineChartRecyclerView);
List<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");
list.add("~~~");
LineChartAdapter lineChartAdapter = new LineChartAdapter(getContext(),
list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setAdapter(lineChartAdapter);
}
#Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
LineChartAdapter.java
public class LineChartAdapter
extends RecyclerView.Adapter<LineChartViewHolder> {
private List<String> mList;
private Context mContext;
public LineChartAdapter(Context context, List<String> list) {
this.mContext = context;
this.mList = list;
}
#Override
public LineChartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext)
.inflate(R.layout.item_line_chart, parent,
false);
return new LineChartViewHolder(itemView);
}
#Override
public void onBindViewHolder(LineChartViewHolder holder, int position) {
holder.mTextView.setText(String.valueOf(position));
}
#Override public int getItemCount() {
return mList == null ? 0 : mList.size();
}
}
LineChartViewHolder.java
public class LineChartViewHolder extends RecyclerView.ViewHolder {
protected TextView mTextView;
public LineChartViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(
R.id.text1024);
}
}
I think, I know the reason.
View v = inflate(getContext(), R.layout.line_chart_recyclerview, null);
LineChart.LayoutParams params = new LineChart.LayoutParams(
getWidth() / 2, getHeight());
params.addRule(LineChart.ALIGN_PARENT_RIGHT);
addView(v, params);
Here, getWidth() and getHeigth() are still 0. So your view will have (0;0) size
To fix this, I'd suggest to replace this part with
LayoutInflater.from(context).inflate(R.layout.line_chart_recyclerview, this);
Let me know, if it helps.
I have a custom spinner
I have a Hint label that is in the last position of my array(spinner), so to display it I set selection to the last position, like this:
ArrayAdapter myAdapter = new MySpinnerAdapter(this,R.layout.spinner_item,createMyList());
myAdapter.setDropDownViewResource(spinner_item);
mySpinner.setAdapter(subCategoryAdapter);
mySpinner.setSelection(myList.size() - 1);
It's working perfectly, but when I touch on Spinner to select a item, the scroll it's "focusing" the bottom of spinner, because of my setSelection.
How can I focus on the first item of the spinner OR "scroll" to the top of it?
Thanks!
You can achieve this by extending Spinner and overriding methods that are responsible for setup and showing the list of values in the drop down:
public class CustomSpinner extends Spinner {
private boolean mToggleFlag = true;
public CustomSpinner(Context context, AttributeSet attrs, int defStyle, int mode) {
super(context, attrs, defStyle, mode);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSpinner(Context context, int mode) {
super(context, mode);
}
public CustomSpinner(Context context) {
super(context);
}
#Override
public int getSelectedItemPosition() {
if (!mToggleFlag) {
return 0; // Gets to the first element
}
return super.getSelectedItemPosition();
}
#Override
public boolean performClick() {
mToggleFlag = false;
boolean result = super.performClick();
mToggleFlag = true;
return result;
}
}
I have a GridView which I am constantly adding views to. When the view adds to the grid I wish for it to do an animation. However, as I have to use setAdapter() to refresh the GridView, it ends up animating all views, as they are all being re-added. Is there anyway around this?
Here is the code for my view I am adding:
public class GridImageView extends ImageView {
public GridImageView(Context context) {
super(context);
}
public GridImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
ScaleAnimation anim = new ScaleAnimation(0,1,0,1);
anim.setDuration(1000);
anim.setFillAfter(true);
this.startAnimation(anim);
}
}
As always, thanks for your help
Thanks to Luksprog's suggestion, I have set a flag in my custom view which will determine if the view should animate when added to the Grid View.
public class GridImageView extends ImageView
{
private boolean _animate = false;
public GridImageView(Context context) {
super(context);
}
public GridImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onAttachedToWindow() {
if(_animate){
super.onAttachedToWindow();
ScaleAnimation anim = new ScaleAnimation(0,1,0,1);
anim.setDuration(1000);
anim.setFillAfter(true);
this.startAnimation(anim);
}
}
public void set_animate(boolean _animate) {
this._animate = _animate;
}
}
and my adapter in its GetView() function checks if it is the last in the array list, and sets the flag to true if so.
if( i == ( _gridDetailsArrayList.size() - 1 ))
holder.gridImage.set_animate(true);