NullPointer void android.widget.LinearLayout.addView(android.view.View)

时间:2015-08-17 14:36:37

标签: java android xml android-layout

我正在制作一个笔记记录应用程序,我正在努力保存已制作的笔记。 我每次都在textview中保存一个布局(textview.xml),片段布局(fragment_second.xml)部分的id为“fragment_second_note” textview.xml

<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>

我的fragment_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);

}

}

完整错误: https://github.com/Heromine/tempapp1/issues/6

其他档案: https://github.com/Heromine/tempapp1

1 个答案:

答案 0 :(得分:1)

您使用错误的引用来获取LinearLayout。因此,请使用以下

替换您的代码
    LinearLayout lLayout = (LinearLayout) rootView .findViewById(R.id.fragment_second_note);
    lLayout.addView(view2);