首页 > 解决方案 > 尝试以编程方式创建表时出现 java.lang.NullPointerException 错误

问题描述

好的,我已经花了几个小时试图解决这个问题,无论我尝试什么,我总是会遇到同样的错误,我认为我错过了一些愚蠢的简单的东西,如果问题已经得到回答,我很抱歉我尝试使用他们的回复但它没有没有帮助。非常感谢所有帮助。

我正在尝试以编程方式将行添加到表中,这是代码:

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>

我得到的错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.coreclue.random5, PID: 21474
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TableRow.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
                  at com.coreclue.random5.tab2.main(tab2.java:31)
                  at com.coreclue.random5.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:106)

我想要得到的只是一个简单的函数,我可以根据需要向表中添加尽可能多的行,但只是尝试添加一行并且它失败了。

标签: javaxmlandroid-fragmentsnullpointerexception

解决方案


终于解决了!在 tab2.java 第 10 行:

public static TableRow tr;

当我需要它时:

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

我假设 var 没有实例化,这就是我遇到错误的原因。


推荐阅读