【问题标题】:NullPointer void android.widget.LinearLayout.addView(android.view.View)NullPointer 无效 android.widget.LinearLayout.addView(android.view.View)
【发布时间】:2015-08-17 14:36:36
【问题描述】:

我正在制作一个笔记应用程序,我正在努力保存所做的笔记。 我每次都在片段布局(fragment_second.xml)部分中使用布局(textview.xml)保存在文本视图中,ID为“fragment_second_note” 文本视图.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

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    您使用错误的参考来获取您的 LinearLayout。所以用下面的代码替换你的代码

    LinearLayout lLayout = (LinearLayout) rootView .findViewById(R.id.fragment_second_note);
    lLayout.addView(view2); 
    

    【讨论】:

    • 但是“视图”在哪里?它也显示相同的错误
    • 你的rootview..抱歉打错了
    猜你喜欢
    • 2023-03-16
    • 2016-06-29
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2019-01-30
    相关资源
    最近更新 更多