Java Android:由于更改某些视图的id标记(使用Eclipse),应用程序崩溃
几周以来,我一直在开发一款由3项活动组成的应用程序。其中2个使用片段。我的项目总共有31个布局文件(XML)。一切正常。。。。。。直到昨天 通过为片段创建布局,我使用了eclipse自动生成的id(textView1、textView2等等)。昨天我想重新命名一些id。但是出了点问题。(我没有按对话框检查预览,因此其他片段和代码部分也发生了更改。我试图删除错误的更改,但似乎我忘记了什么,或者只是不知道什么) 当我拖动片段时一切都很好,但是如果我想更改某些内容(例如TextView上的setText()),应用程序就会崩溃。看起来它找不到视图(“空对象引用”),但从形式上来说,应用程序已经通过id找到了视图 下面是错误日志:(我用注释标记了关键部分) 以下是活动的来源(略去部分): 活动布局:Java Android:由于更改某些视图的id标记(使用Eclipse),应用程序崩溃,java,android,eclipse,Java,Android,Eclipse,几周以来,我一直在开发一款由3项活动组成的应用程序。其中2个使用片段。我的项目总共有31个布局文件(XML)。一切正常。。。。。。直到昨天 通过为片段创建布局,我使用了eclipse自动生成的id(textView1、textView2等等)。昨天我想重新命名一些id。但是出了点问题。(我没有按对话框检查预览,因此其他片段和代码部分也发生了更改。我试图删除错误的更改,但似乎我忘记了什么,或者只是不知道什么) 当我拖动片段时一切都很好,但是如果我想更改某些内容(例如TextView上的setTex
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
tools:context="com.nobody.allstuffapp.LoginActivity" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/mytoolbar" />
<ScrollView
android:id="@+id/login_contentframe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
android:scrollbarStyle="outsideInset" >
</ScrollView>
</FrameLayout>
下面是片段_agb1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:id="@+id/lay_agb1" >
<TextView
android:id="@+id/tv_agb1_agbcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="0dp"
android:text="@string/txt_agb1"
android:textSize="14sp" />
<RelativeLayout
android:id="@+id/lay_agb1_button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginTop="20dp"
android:gravity="center" >
<ImageView
android:id="@+id/iv_agb1_button_back"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:contentDescription="@string/str_cont_register"
android:onClick="HomeShow"
android:scaleType="centerInside"
android:src="@drawable/bz_main_nobanner" />
<TextView
android:id="@+id/tv_agb1_buttoncaption_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/str_login_back"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</RelativeLayout>
<TextView
android:id="@+id/tv_agb1_copyright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="@string/str_login_copyright"
android:textSize="12sp" />
</LinearLayout>
希望你们中有人能帮助我。我还没找到问题。
对不起,我的英语不好
thx
编辑:===================================================================
我想我发现了问题,但如何解决呢??:
解释:如果有两个布局文件具有名为“TextView1”的TextView,那么这两个布局文件在R-File中都具有相同的资源ID。如果我更改其中一个布局中的名称,那么它也会更改另一个布局中的名称。如果使用“更新引用”对话框的预览,只选择要更改的布局,而java文件使用引用,而不选择要更新的R文件,则生成错误原因。但是,如果我选择要更新的r文件,那么另一个布局的对象也会以同样的方式失去其引用。对吧?
现在的问题是:我如何重命名textview(按id)和Eclipse add的新资源int而不是重命名现有的,它现在已存在于其他对象中???您发布的第二个布局是“fragment\u agb1.xml”,但在Activity\u login.xml中,您包含了布局“mytoolbar”。这可能就是问题所在 你能核实一下名字吗 ((文本视图) findViewById(R.id.lay_agb1)。findViewById(R.id.tv_agb1_agbcontent)).setText(“你好,世界!!!” 尝试将此行替换为:
((TextView)agb1Fragment.getView().findViewById(R.id.tv_agb1_agbcontent)).setText("Hello World!!!");
编辑。
尝试在
AGB1Fragment
类中创建TextView
的public
引用:
public TextView tv_content;
onCreateView(...) {
...
tv_content = (TextView) rootView.findViewById(R.id.tv_agb1_agbcontent);
...
}
然后使用它:
agb1Fragment.tv_content.setText("Wow");
你清理过你的项目吗?是的,不止一次…检查导入的R.是否是你的R.例如R.com.example.yourpackage而不是R.android.xxx…似乎是R.我可以重现问题,但是我如何在一个布局中重命名一个视图并更改R,而不更改另一个布局中的视图(在R中使用相同的名称)。它至少应该有两个不同的资源int,但它不是由eclipse构建的。。。。明白我的意思吗?我将在这里发表一篇新文章,试图说明这个问题……最好的解决方案是在Android Studio中导入该项目,并在那里重命名ID。androidstudio在重构方面做得更好,而Eclipse相比之下则是一团乱麻。它类似于第二个动作条。activity.java加载fragment_agb1.xml以登录_contentframe(滚动框)。您的编辑:结果相同。空对象。好像是R档案。请参阅主帖子中的“我的编辑”。@Justl24在“图形布局”中打开布局,找到视图-上下文菜单-编辑IDsorry,上下文与在属性列表中编辑的方式相同,因此存在相同的问题。。。
public TextView tv_content;
onCreateView(...) {
...
tv_content = (TextView) rootView.findViewById(R.id.tv_agb1_agbcontent);
...
}
agb1Fragment.tv_content.setText("Wow");