Java Android:调用findviewbyid时出现空异常
我有一个问题,当我在TextView中调用findViewById时抛出NullPointerException,但对其他元素则没有。我尝试过任何事情(更改标识符、调用OnCreateView、清理和构建项目),但我不知道该怎么做 这是我的活动Java Android:调用findviewbyid时出现空异常,java,android,Java,Android,我有一个问题,当我在TextView中调用findViewById时抛出NullPointerException,但对其他元素则没有。我尝试过任何事情(更改标识符、调用OnCreateView、清理和构建项目),但我不知道该怎么做 这是我的活动 public class RoutesActivity extends Activity { ListView routesList; EditText searchTxt; TextView noneRouteTxt; long uu
public class RoutesActivity extends Activity {
ListView routesList;
EditText searchTxt;
TextView noneRouteTxt;
long uuid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routes);
noneRouteTxt = (TextView)this.findViewById(R.id.notroute_txt);
getControls();
Bundle extra = this.getIntent().getExtras();
getRoutesList(extra);
}
这是我的布局:activity_routes.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.geochild.app.com.geochild.app.activitities.RoutesActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="325dp"
android:background="@drawable/app_background_waves"
android:layout_above="@+id/linearLayout"
android:layout_below="@+id/header_layout">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/routes_listview"
android:visibility="invisible">
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_routes"
android:id="@+id/notroute_txt"
android:visibility="invisible"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
你能帮我吗?谢谢NPE显然会在下面的一行中抛出。但是您还应该显示堆栈跟踪的第一行
noneRouteTxt = (TextView)this.findViewById(R.id.notroute_txt);
引发NPE的原因是,应用程序在调用onCreate()方法时使用的布局是另一个布局,而不是“activity_routes”,其中id notroute_txt不存在
您正在使用或在res文件夹中复制了多少布局?发布堆栈跟踪,并指出代码中的哪一行被引用。请确保对于res/layout、res/layout-v14等中的每个布局“活动路径”都没有定义路径。否则,如果它使用的布局没有NPE,您将得到NPE。