java - fragment 中级 (II) :Dynmically adding row in a fragment, Android

标签 java android android-fragments android-tablelayout

我正在尝试向 fragment 中的表格动态添加行。 但是,我遇到了一些运行时错误,非常需要一些建议。

日志:

1)E/AndroidRuntime(1038): java.lang.RuntimeException: 无法启动 Activity ComponentInfo{com.example.lol/com.example.lol.MainActivity}: android.view.InflateException: Binary XML文件行 #96:扩展类 fragment 时出错

2)E/AndroidRuntime(1038): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)

3)E/AndroidRuntime(1038):at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2084)

FragmentTwo.Java

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class FragmentTwo extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.fragment_two, container, false);

        TableLayout tl=(TableLayout) getActivity().findViewById(R.id.mainLayout);


        TableRow tr = new TableRow(getActivity().getApplicationContext());
//        tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));


        TextView textview1 = new TextView(getActivity().getApplicationContext());
        textview1.setText("unhappy");
        tr.addView(textview1);

        TextView textview2 = new TextView(getActivity().getApplicationContext());
        textview2.setText("happy");
        tr.addView(textview2);


        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        return view;
    }

主 Activity .java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void selectFrag(View view) {
        Fragment fr;    
        if(view == findViewById(R.id.button2)) {
            fr = new FragmentTwo();

        }else {
            fr = new FragmentOne();
        }
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_place, fr);
        fragmentTransaction.commit();

   }

}

fragment _二.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:orientation="vertical"
   android:background="#ffff00">


        <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mainLayout">

        <TableRow
                android:id="@+id/infoRow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                        android:text="Unhappy"
                        android:id="@+id/column1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                </TextView>

                <TextView
                        android:text="Happy"
                        android:id="@+id/column2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                </TextView>


        </TableRow>
</TableLayout>
</LinearLayout>

最佳答案

改变这个

TableLayout tl=(TableLayout) getActivity().findViewById(R.id.mainLayout);

TableLayout tl=(TableLayout) view.findViewById(R.id.mainLayout);

这是一个避免 NullPointerException 的错误。

并使用getActivity() 来初始化 View 。要知道为什么阅读

When to call activity context OR application context?

关于java - fragment 中级 (II) :Dynmically adding row in a fragment, Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22767439/

相关文章:

java - 如何确保Android应用程序正在访问真实服务器

java - 为什么 Eclipse 控制台中的一些输出是红色的?

java - 是否有与 SignalR 等效的 Java?

android - 原始资源文件类型

android - 使用导航组件时不设置 Fragment 的 id 和 tag

java - 使用 power mockito 抑制私有(private)静态方法

android - Android/iPhone 中的CSS

android - 如何使用 android ndk 的原生相机库?

java - 在 Fragment Pager Adapter 中保留 Fragment 的实例状态

android - 防止同一 fragment 多次堆叠 (addToBackStack)