-1

guys! I had problem with this error java: cannot find symbol in my android application project recently. I don't know where is the error that made the problem. Here is my file details:

navigation_bar_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/navigation_bar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#515151"
            android:layout_alignParentTop="true">
<!--Back-->
<ImageView android:id="@+id/back_icon"
           android:layout_width="50dp"
           android:layout_height="50dp"
           android:padding="5dp"
           android:layout_centerVertical="true"
           android:layout_alignParentLeft="true"
           android:src="@drawable/back_icon"/>
<!--Title-->
<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>
<!--Menu-->
<ImageView
        android:id="@+id/menu"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="5dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>
</RelativeLayout>

NavigationBar.java :

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;    

public class NavigationBar extends RelativeLayout implements OnClickListener{
public NavigationBar(Context context){
    this(context, null);
}

private ImageView backImageView;
private TextView titleView;
private ImageView menuImageView;

public NavigationBar(Context context, AttributeSet attrs){
    super(context, attrs);

    View view     = LayoutInflater.from(context).inflate(R.layout.navigation_bar_layout, this, true);
    backImageView = (ImageView) view.findViewById(R.id.back_icon);
    titleView     = (TextView) view.findViewById(R.id.title);
    menuImageView = (ImageView) view.findViewById(R.id.menu);

    backImageView.setOnClickListener(this);
    menuImageView.setOnClickListener(this);
}


public ImageView getBackImageView(){ return backImageView; }
public TextView getTitleView(){ return titleView; }
public ImageView getMenuImageView(){ return menuImageView; }

public void setTitle(String title){ titleView.setText(title); }

private ClickCallBack clickCallBack;
public void setClickCallBack(ClickCallBack clickCallBack){ this.clickCallBack = clickCallBack; }
public static interface ClickCallBack{
    void onBackClick();     
    void onMenuClick();     
}

@Override
public void onClick(View v) {
    int id = v.getId();
    if(id == R.id.back_icon){
        clickCallBack.onBackClick();
        return;
    }
    if(id == R.id.menu){
        clickCallBack.onMenuClick();
        return;
    }
  }
}

And, here is error console :

Error:(24, 9) java: cannot find symbol
symbol:   variable backImageView
location: class com.example.MyFirstAndroidApp.NavigationBar
Error:(24, 37) java: cannot find symbol
symbol:   variable view
location: class com.example.MyFirstAndroidApp.NavigationBar
Error:(25, 36) java: cannot find symbol
symbol:   variable view
location: class com.example.MyFirstAndroidApp.NavigationBar
Error:(26, 37) java: cannot find symbol
symbol:   variable view
location: class com.example.MyFirstAndroidApp.NavigationBar

Any ideas?

frank jorsn
  • 489
  • 1
  • 9
  • 27

1 Answers1

2

LayoutInflater is the term used within the context of Android to indicate when an XML layout resource is parsed and converted into a hierarchy of View objects.

You should try with

 View view = LayoutInflater.from(context).inflate(R.layout.navigation_bar_layout,null);

Then Clean-Rebuild Your Project .

For your Second question , You should check Layout Inflation as Intended

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    Well, although I have not a absolute clear understanding of the difference between them, and I will take a lot of time to read the API carefully. Anyway, thanks so much! – frank jorsn Mar 18 '16 at 08:48