家伙!我最近在我的android应用程序项目中遇到此错误 java:找不到符号 的问题。我不知道导致问题的错误在哪里。这是我的文件详细信息:
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;
}
}
}
并且,这是 错误控制台 :
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
有什么想法吗?
答案 0 :(得分:2)
LayoutInflater 是在Android上下文中使用的术语,用于指示何时解析XML布局资源并将其转换为View对象的层次结构。
你应该试试
View view = LayoutInflater.from(context).inflate(R.layout.navigation_bar_layout,null);
然后 Clean-Rebuild
您的项目。
对于第二个问题,您应该检查 Layout Inflation as Intended