Java 空指针void android.widget.LinearLayout.addView(android.view.view)
我正在制作一个笔记应用程序,我正在保存所做的笔记。 我每次都在文本视图中保存一个布局textview.xml,该布局位于id为fragment\u second\u注释的fragment layout fragment\u second.xml部分 textview.xmlJava 空指针void android.widget.LinearLayout.addView(android.view.view),java,android,xml,android-layout,Java,Android,Xml,Android Layout,我正在制作一个笔记应用程序,我正在保存所做的笔记。 我每次都在文本视图中保存一个布局textview.xml,该布局位于id为fragment\u second\u注释的fragment layout fragment\u second.xml部分 textview.xml <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textview1" android:text
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview1"
android:text="@string/Press_to_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#e9ff23" />
fragment_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment"
android:transitionGroup="false">
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add"
android:elevation="5dp"
app:backgroundTint="@color/minecraft_brown_dirt_darkest"
app:borderWidth="0dp"
android:layout_marginBottom="15dp"
android:layout_marginEnd="13dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_second_note" />
</LinearLayout>
</RelativeLayout>
我的片段_second.java
public class Fragment_second extends android.support.v4.app.Fragment implements OnClickListener {
FloatingActionButton fab;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second, container, false);
NotesDatabase db = new NotesDatabase(getActivity());
List<Notes> notes = db.getAllNotes();
for (Notes n :notes) {
final LayoutInflater inflater2 = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView view2 = (TextView) inflater2.inflate(R.layout.textview,null);
view2.setText(n.getNote());
LinearLayout lLayout = (LinearLayout) view2.findViewById(R.id.fragment_second_note);
lLayout.addView(view2); }
fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
fab.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View view) {
//on fab's click actions to perform here:
Snackbar.make(view, "Sto creando una nuova nota...", Snackbar.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), EditNoteActivity.class);
startActivity(intent);
/*
Toast.makeText(getActivity().getApplicationContext(),
"Creando una nuova nota...", Toast.LENGTH_SHORT).show();
*/
//LayoutInflater inflater = LayoutInflater.from(getActivity());
//inflater.inflate(R.layout.car dview, null);
// Intent intent = new Intent(getActivity(), EditNoteg.class);
//startActivity(intent);
}
}
完全错误:
其他文件:
您使用了错误的参考来获得线性布局。因此,用以下代码替换您的代码
LinearLayout lLayout = (LinearLayout) rootView .findViewById(R.id.fragment_second_note);
lLayout.addView(view2);
但风景在哪里?它也显示了相同的错误您的rootview..很抱歉这是一个打字错误