Java 为什么不是我的;关于「;在R中识别的标签
我正在尝试使用本教程为我自己的教育目的创建的应用程序实现一个对话框 在我的notepadlistactivity.java中有一节Java 为什么不是我的;关于「;在R中识别的标签,java,android,android-fragments,android-fragmentactivity,Java,Android,Android Fragments,Android Fragmentactivity,我正在尝试使用本教程为我自己的教育目的创建的应用程序实现一个对话框 在我的notepadlistactivity.java中有一节 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.addNote: openNote(new Note(noteManage
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.addNote:
openNote(new Note(noteManager));
break;
case android.R.id.home:
break;
case android.R.id.about:
FragmentManager fm = getFragmentManager();
MyDialogFragment dialogFragment = new MyDialogFragment ();
dialogFragment.show(fm, "Sample Fragment");
default:
return false;
}
return true;
}
对于所有其他R.id…,它识别点后的单词,但是对于R.id.about,不识别about。我已经签入了R文件,并且定义了about,因此我无法理解出了什么问题
请参考我的MyDialogFragment.java
package com.nicae.Notepad.note_list;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.nicae.Notepad.R;
public class MyDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sample_dialog, container, false);
getDialog().setTitle("Simple Dialog");
return rootView;
Button dismiss = (Button) rootView.findViewById(R.id.dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
这是我的fragment\u sample\u dialog.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="match_parent"
android:gravity="center"
android:padding="10dp"
android:orientation="vertical"
android:id="@+id/about">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet..."
android:textSize="20dp" />
<Button
android:id="@+id/dismiss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
</LinearLayout>
编辑与以下注释相对应的内容:
这是我的活动\u notepad\u list.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/addNote"
android:title="@string/newNote"
android:orderInCategory="1"
app:showAsAction="always"
android:icon="@drawable/content_new"/>
<item
android:id="@+id/about"
android:title="About"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_ab_other"
android:orderInCategory="2"/>
</menu>
注意,android.R.id
与R.id
不同,这取决于当前类所在的当前包名和/或import
语句android.R.id
指的是来自android API的标准值。您可能打算改用R.id
。这是因为您对LinearLayout和菜单项使用相同的id。从LinearLayout中删除android:id=“@+id/about”
(或将其更改为“@+id/aboutLayout”
)
使用,
case R.id.about: // instead of android.R.id.about
FragmentManager fm = getFragmentManager();
MyDialogFragment dialogFragment = new MyDialogFragment ();
dialogFragment.show(fm, "Sample Fragment");
在您创建“关于”的地方,由于问题发生在选项项Selected()中,我假定您正在管理菜单或操作栏单击。请发布定义菜单的XML文件。请参阅我的答案。(另外,我认为你的片段中的
线性布局不需要android:id
。如果你真的需要这个id
,它应该与有关的有所不同,因为这会导致名称与菜单冲突。)@Jurassic,《代码学徒》给了你一个答案。