setContentView(R.layout.activity_main) Error解决方法

今天在写Android代码的过程中,编译器一直报错,错误出在这一行代码:

<span class="pln" style="margin: 0px; padding: 0px;">setContentView</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px;">R</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px;">layout</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px;">activity_main</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">)</span>

提示信息是:
activity_main cannot be resolved or is not a field
我就觉得很奇怪,我在R.layout里面明明看到自动生成了默认的布局activity_main,为什么在这里引用就出错呢。Google之也没发现原因,而且,如果删掉activity_main,Eclipse给出的自动提示是activity_list_item.我当时就觉得可能是导入的某个包覆盖了我真正要引用的R.java.
然后在import里面果然找到了这么一句:

<span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">import</span><span class="pln" style="margin: 0px; padding: 0px;"> android</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px;">R</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span>

删掉这一句就正常了。
出现这个问题的原因是,如果你在R.java还没有来得及自动生成的时候选择了fix imports,那么Eclipse就会自动帮你导入android.R这个包,就会覆盖掉本地的R.java,所以就导致了你不能正常引用R.layout.activity_main.

请用JAVA语言帮我重构package com.shhg; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private EditText urlInput; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 显示视频播放对话框 showVideoDialog(); } /** * 显示视频播放对话框 */ private void showVideoDialog() { View dialogView = View.inflate(this, R.layout.activity_main, null); urlInput = dialogView.findViewById(R.id.et_url); Button playButton = dialogView.findViewById(R.id.btn_play); AlertDialog dialog = new AlertDialog.Builder(this) .setTitle("视频播放器") .setView(dialogView) .setCancelable(true) .create(); // 设置点击事件 playButton.setOnClickListener(v -> { String videoUrl = urlInput.getText().toString().trim(); if (!videoUrl.isEmpty()) { playVideo(videoUrl); dialog.dismiss(); // 关闭对话框 } else { urlInput.setError("请输入视频URL"); } }); dialog.show(); } /** * 处理播放按钮点击事件 */ private void handlePlayButtonClick(AlertDialog dialog) { String videoUrl = urlInput.getText().toString().trim(); if (videoUrl.isEmpty()) { urlInput.setError("请输入视频URL"); return; } if (isValidUrl(videoUrl)) { playVideo(videoUrl); dialog.dismiss(); } else { showErrorMessage("无效URL", "请输入有效的视频链接"); } } /** * 播放视频 */ private void playVideo(String videoUrl) { try { Uri uri = Uri.parse(videoUrl); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { showErrorMessage("错误", "未找到可用的视频播放器"); } } catch (Exception e) { showErrorMessage("无效URL", "请输入有效的视频链接\n错误信息: " + e.getMessage()); } } /** * 显示错误提示对话框 */ private void showErrorMessage(String title, String message) { new AlertDialog.Builder(this) .setTitle(title) .setMessage(message) .setPositiveButton("确定", null) .show(); } }
最新发布
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值