我有一个问题,当我在TextView中调用findViewById时抛出NullPointerException,但不会抛出其他元素。我尝试过任何事情(更改标识符,调用OnCreateView,清理和构建项目)并且我不知道该怎么做。
这是我的活动
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>
你能帮帮我吗?感谢
答案 0 :(得分:0)
NPE显然会被抛入以下行。但是你也应该提出堆栈跟踪的第一行。
noneRouteTxt = (TextView)this.findViewById(R.id.notroute_txt);
并抛出NPE,因为应用程序在调用onCreate()方法时使用的布局是另一个而不是&#39; activity_routes&#39;,其中id notroute_txt不存在。
您在res文件夹中使用了多少个布局或已复制?