Android求助,findViewById无法找到布局中建立的id

青枭 2015-12-13 03:51:54
findViewById无法找到布局中建立的id
activity_main.xml中的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/start_normal_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start NormalActivity" />

<Button
android:id="@+id/start_dialog_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start DialogActivity" />

</LinearLayout>

MainActivity中的代码:
public class MainActivity extends Activity {

public static final String TAG="MainActivity";

@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
Log.d(TAG,"onCreate");
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

Button startNormalActivity=(Button) findViewById(R.id.start_normal_activity);
}
...全文
430 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
水的川 2015-12-13
  • 打赏
  • 举报
回复
引用 5 楼 u012870504 的回复:
引用 1 楼 u013377714 的回复:
亲,你怎么知道没找到对应的id呢?你调用了吗?你的错误log呢?
郁闷 clean下就可以了 我说怎么都找不到错的 谢谢你的回复
你现在再看你上面的import是不是多出了一个import关于R文件的。之前错误是没自动生成,可能你没把eclipse设成自动编译。
青枭 2015-12-13
  • 打赏
  • 举报
回复
引用 1 楼 u013377714 的回复:
亲,你怎么知道没找到对应的id呢?你调用了吗?你的错误log呢?
郁闷 clean下就可以了 我说怎么都找不到错的 谢谢你的回复
青枭 2015-12-13
  • 打赏
  • 举报
回复
引用 3 楼 u013377714 的回复:
[quote=引用 2 楼 u012870504 的回复:] [quote=引用 1 楼 u013377714 的回复:] 亲,你怎么知道没找到对应的id呢?你调用了吗?你的错误log呢?
打findViewById的时候 在括号里大R.id. 后面不是会弹出已有的id吗? 但是我这没有 将在布局里的Button的id start_normal_activity 手动打进去 错误提示 Multiple markers at this line - start_normal_activity cannot be resolved or is not a field - start_normal_activity cannot be resolved or is not a field[/quote]因为你import进去的R文件有问题,你把系统自带的R引用进去了。[/quote]
引用 3 楼 u013377714 的回复:
[quote=引用 2 楼 u012870504 的回复:] [quote=引用 1 楼 u013377714 的回复:] 亲,你怎么知道没找到对应的id呢?你调用了吗?你的错误log呢?
打findViewById的时候 在括号里大R.id. 后面不是会弹出已有的id吗? 但是我这没有 将在布局里的Button的id start_normal_activity 手动打进去 错误提示 Multiple markers at this line - start_normal_activity cannot be resolved or is not a field - start_normal_activity cannot be resolved or is not a field[/quote]因为你import进去的R文件有问题,你把系统自带的R引用进去了。[/quote] 什么意思?import是指MainActivity的上面那段吗? package com.example.activitylifecycletest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; 我原来新建activity_main的时候是自动生成的 原来不是LinearLayout的 我照着书上改的
水的川 2015-12-13
  • 打赏
  • 举报
回复
引用 2 楼 u012870504 的回复:
[quote=引用 1 楼 u013377714 的回复:] 亲,你怎么知道没找到对应的id呢?你调用了吗?你的错误log呢?
打findViewById的时候 在括号里大R.id. 后面不是会弹出已有的id吗? 但是我这没有 将在布局里的Button的id start_normal_activity 手动打进去 错误提示 Multiple markers at this line - start_normal_activity cannot be resolved or is not a field - start_normal_activity cannot be resolved or is not a field[/quote]因为你import进去的R文件有问题,你把系统自带的R引用进去了。
青枭 2015-12-13
  • 打赏
  • 举报
回复
引用 1 楼 u013377714 的回复:
亲,你怎么知道没找到对应的id呢?你调用了吗?你的错误log呢?
打findViewById的时候 在括号里大R.id. 后面不是会弹出已有的id吗? 但是我这没有 将在布局里的Button的id start_normal_activity 手动打进去 错误提示 Multiple markers at this line - start_normal_activity cannot be resolved or is not a field - start_normal_activity cannot be resolved or is not a field
水的川 2015-12-13
  • 打赏
  • 举报
回复
亲,你怎么知道没找到对应的id呢?你调用了吗?你的错误log呢?

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧