Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试以编程方式创建表时,java.lang.NullPointerException出错_Java_Xml_Android Fragments_Nullpointerexception - Fatal编程技术网

尝试以编程方式创建表时,java.lang.NullPointerException出错

尝试以编程方式创建表时,java.lang.NullPointerException出错,java,xml,android-fragments,nullpointerexception,Java,Xml,Android Fragments,Nullpointerexception,好吧,我花了好几个小时试图解决这个问题,不管我怎么做,我总是会遇到同样的错误,我认为我错过了一些非常简单的东西,所以很抱歉,如果问题已经得到了回答,我尝试使用他们的回答,但没有任何帮助。非常感谢您的帮助 我试图以编程方式向表中添加行,代码如下: MainActivity.java: package com.coreclue.random5; import android.support.design.widget.TabLayout; import android.support.design.

好吧,我花了好几个小时试图解决这个问题,不管我怎么做,我总是会遇到同样的错误,我认为我错过了一些非常简单的东西,所以很抱歉,如果问题已经得到了回答,我尝试使用他们的回答,但没有任何帮助。非常感谢您的帮助

我试图以编程方式向表中添加行,代码如下:

MainActivity.java:

package com.coreclue.random5;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableRow;
public class MainActivity extends AppCompatActivity{
  private SectionsPagerAdapter mSectionsPagerAdapter;
  private ViewPager mViewPager;
  public static Button b;
  public static TableRow tr;
  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tr = new TableRow(this);
    b = new Button(this);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    mViewPager.addOnPageChangeListener(new     TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new     TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener(){
      @Override
      public void onClick(View view){
        Snackbar.make(view, "test test test 123",     Snackbar.LENGTH_LONG).setAction("Action", null).show();
      }
    });
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();
    if(id == R.id.action_settings){
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
  public static class PlaceholderFragment extends Fragment{
    private static final String ARG_SECTION_NUMBER = "section_number";
    //public PlaceholderFragment(){
    //}
    public static PlaceholderFragment newInstance(int sectionNumber){
      PlaceholderFragment fragment = new PlaceholderFragment();
      Bundle args = new Bundle();
      args.putInt(ARG_SECTION_NUMBER, sectionNumber);
      fragment.setArguments(args);
      return fragment;
    }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
  View tabView1 = inflater.inflate(R.layout.tab1, container, false);
  View tabView2 = inflater.inflate(R.layout.tab2, container, false);
  View tabView3 = inflater.inflate(R.layout.tab3, container, false);
  if(getArguments().getInt(ARG_SECTION_NUMBER) == 1){
    tab1 obj = new tab1();
    obj.main(tabView1);
    return tabView1;
  } else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
    tab2 obj = new tab2();
    obj.main(tabView2);
    return tabView2;
  } else if(getArguments().getInt(ARG_SECTION_NUMBER) == 3){
    return tabView3;
  } else{
    return tabView1;
  }
}
  }
  public class SectionsPagerAdapter extends FragmentPagerAdapter{
    public SectionsPagerAdapter(FragmentManager fm){
      super(fm);
    }
    @Override
    public Fragment getItem(int position){
      return PlaceholderFragment.newInstance(position + 1);
    }
    @Override
    public int getCount(){
      return 3;
    }
  }
}
tab2.java,我想在其中编写代码,将行添加到表中:

package com.coreclue.random5;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import static com.coreclue.random5.MainActivity.b;
public class tab2{
  public static TableRow tr;
  public void main(View rootView){
    final TextView line1 = rootView.findViewById(R.id.line1);
    line1.setText("aa");
    TableLayout tl = (TableLayout) rootView.findViewById(R.id.table123);
    tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    b.setText("Dynamic Button");
    b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    tr.addView(b);
    tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
  }
}
tab2.xml:表所在的位置

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/table123"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="1">
        <TableRow>
            <TextView
                android:id="@+id/line1"
                android:padding="3dip"
                android:text="text 1" />
            <TextView
                android:gravity="right"
                android:padding="3dip"
                android:text="text 2" />
        </TableRow>
        <TableRow>
            <TextView
                android:padding="3dip"
                android:text="text 3" />
            <TextView
                android:gravity="right"
                android:padding="3dip"
                android:text="text 4" />
        </TableRow>
    </TableLayout>
  </LinearLayout>
</ScrollView>

我想得到的只是一个简单的函数,在这个函数中,我可以根据需要向表中添加任意多的行,但仅仅添加一行就失败了。

最终解决了这个问题!在tab2.java第10行:

public static TableRow tr;
当我需要的时候:

import static com.coreclue.random5.MainActivity.tr;
我假设var没有实例化,这就是我出错的原因

import static com.coreclue.random5.MainActivity.tr;