空指针异常,android.widget.Tablayout.getChildAt(int)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了空指针异常,android.widget.Tablayout.getChildAt(int)相关的知识,希望对你有一定的参考价值。

我是android开发的新手,当我点击我的应用程序时它停止工作,因为它无法破坏表格布局......................... .................................................. .................................................. .................................................. ........................这是我在调试器控制台中得到的

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.mohannad.athkarapp, PID: 7139
              java.lang.RuntimeException: Unable to destroy activity      {com.mohannad.athkarapp/com.mohannad.athkar`enter code here`app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TableLayout.getChildAt(int)' on a null object reference
                  at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4203)

这是我的代码:

public class tab2fragment extends Fragment {
public static TableLayout tableLayout;
public static TableRow tableRow;
public static TextView id0Count;
public static TextView id1Count;
public static TextView id2Count;
public static TextView id3Count;
public static TextView id4Count;

public tab2fragment() {
    // Required empty public constructor
}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

   refreshF();

}

@Override
public void onStart() {
    super.onStart();
    refreshF();
}

public void refreshF(){

    tableLayout = (TableLayout) getActivity().findViewById(R.id.ourTable);
    tableRow = (TableRow) tableLayout.getChildAt(1);
    id0Count = (TextView) tableRow.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(2);
    id1Count = (TextView) tableRow.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(3);
    id2Count = (TextView) tableRow.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(4);
    id3Count = (TextView) tableRow.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(5);
    id4Count = (TextView) tableRow.getChildAt(0);
    MainActivity.CountZ1= getActivity().getSharedPreferences(MainActivity.zekr1,0);
    MainActivity.CountZ2 = getActivity().getSharedPreferences(MainActivity.zekr2,0);
    MainActivity.CountZ3 = getActivity().getSharedPreferences(MainActivity.zekr3,0);
    MainActivity.CountZ4 = getActivity().getSharedPreferences(MainActivity.zekr4,0);
    MainActivity.CountZ5 = getActivity().getSharedPreferences(MainActivity.zekr5,0);
    id0Count.setText(""+MainActivity.CountZ1.getInt(MainActivity.zekr1,0));
    id1Count.setText(""+MainActivity.CountZ2.getInt(MainActivity.zekr2,0));
    id2Count.setText(""+MainActivity.CountZ3.getInt(MainActivity.zekr3,0));
    id3Count.setText(""+MainActivity.CountZ4.getInt(MainActivity.zekr4,0));
    id3Count.setText(""+MainActivity.CountZ5.getInt(MainActivity.zekr5,0));
}
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.tab2fragment, container, false);
}
@Override
public void onResume() {

    super.onResume();
    refreshF();
}



@Override
public void onDetach() {
    super.onDetach();
    refreshF();
}

}

public class MainActivity extends AppCompatActivity {
public static final String switchp = "Switchpref";
public static final String radio = "radioPref";
public static final String zekr1 = "MyPrefsFileZ1";
public static final String zekr2 = "MyPrefsFileZ2";
public static final String zekr3 = "MyPrefsFileZ3";
public static final String zekr4 = "MyPrefsFileZ4";
public static final String zekr5 = "MyPrefsFileZ5";
public static SharedPreferences CountZ1;
public static SharedPreferences CountZ2;
public static SharedPreferences CountZ3;
public static SharedPreferences CountZ4;
public static SharedPreferences CountZ5;
public static SharedPreferences switchStatus;
public static SharedPreferences radStatus;

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    // Add Fragments to adapter one by one
    adapter.addFragment(new tab1fragment(), "المسبحة");
    adapter.addFragment(new tab2fragment(), "السجل");
    adapter.addFragment(new tab3fragment(), "الإعدادات");
    viewPager.setAdapter(adapter);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);


}

public void zerothat(View view) {
    TextView v= findViewById(R.id.textcount);
    tab1fragment.spinner = (Spinner) findViewById(R.id.spinner);
    doVibrate(200);
    SharedPreferences.Editor editor1= MainActivity.CountZ1.edit();
    SharedPreferences.Editor editor2= MainActivity.CountZ2.edit();
    SharedPreferences.Editor editor3= MainActivity.CountZ3.edit();
    SharedPreferences.Editor editor4= MainActivity.CountZ4.edit();
    SharedPreferences.Editor editor5= MainActivity.CountZ5.edit();
    int pos=tab1fragment.spinner.getSelectedItemPosition();
    switch (pos){
        case 0:tab1fragment.count1=0;
            tab2fragment.id0Count.setText(""+0);
            editor1= MainActivity.CountZ1.edit();
            editor1.putInt(MainActivity.zekr1,0);
            editor1.commit();
            v= findViewById(R.id.textcount);
            v.setText(""+MainActivity.CountZ1.getInt(MainActivity.zekr1,0));
            break;
        case 1: tab1fragment.count2=0;
            tab2fragment.id1Count.setText(""+0);
            editor2= MainActivity.CountZ2.edit();
            editor2.putInt(MainActivity.zekr2,0);
            editor2.commit();
            v= findViewById(R.id.textcount);
            v.setText(""+MainActivity.CountZ2.getInt(MainActivity.zekr2,0));
            break;
        case 2: tab1fragment.count3=0;
            tab2fragment.id2Count.setText(""+0);
            editor3= MainActivity.CountZ3.edit();
            editor3.putInt(MainActivity.zekr3,0);
            editor3.commit();
            v= findViewById(R.id.textcount);
            v.setText(""+MainActivity.CountZ3.getInt(MainActivity.zekr3,0));
            break;
        case 3: tab1fragment.count4=0;
            tab2fragment.id3Count.setText(""+0);
            editor4= MainActivity.CountZ4.edit();
            editor4.putInt(MainActivity.zekr4,0);
            editor4.commit();
            v= findViewById(R.id.textcount);
            v.setText(""+MainActivity.CountZ4.getInt(MainActivity.zekr4,0));
            break;
        case 4: tab1fragment.count5=0;
            tab2fragment.id4Count.setText(""+0);
            editor5= MainActivity.CountZ5.edit();
            editor5.putInt(MainActivity.zekr5,0);
            editor5.commit();
            v= findViewById(R.id.textcount);
            v.setText(""+MainActivity.CountZ5.getInt(MainActivity.zekr5,0));
            break;
    }

}


// Adapter for the viewpager using FragmentPagerAdapter
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
void doVibrate(Integer num){
    Vibrator x = (Vibrator) getSystemService(this.VIBRATOR_SERVICE);
    x.vibrate(num);
}

}

答案

我提供了一种快速解决方案,但我也建议你浏览片段和活动生命周期的Android基础知识。

tableLayout = (TableLayout) getActivity().findViewById(R.id.ourTable);
if(tableLayout != null ){
tableRow = (TableRow) tableLayout.getChildAt(1);
id0Count = (TextView) tableRow.getChildAt(0);
tableRow = (TableRow) tableLayout.getChildAt(2);
id1Count = (TextView) tableRow.getChildAt(0);
tableRow = (TableRow) tableLayout.getChildAt(3);
id2Count = (TextView) tableRow.getChildAt(0);
tableRow = (TableRow) tableLayout.getChildAt(4);
id3Count = (TextView) tableRow.getChildAt(0);
tableRow = (TableRow) tableLayout.getChildAt(5);
id4Count = (TextView) tableRow.getChildAt(0);
MainActivity.CountZ1= getActivity().getSharedPreferences(MainActivity.zekr1,0);
MainActivity.CountZ2 = getActivity().getSharedPreferences(MainActivity.zekr2,0);
MainActivity.CountZ3 = getActivity().getSharedPreferences(MainActivity.zekr3,0);
MainActivity.CountZ4 = getActivity().getSharedPreferences(MainActivity.zekr4,0);
MainActivity.CountZ5 = getActivity().getSharedPreferences(MainActivity.zekr5,0);
id0Count.setText(""+MainActivity.CountZ1.getInt(MainActivity.zekr1,0));
id1Count.setText(""+MainActivity.CountZ2.getInt(MainActivity.zekr2,0));
id2Count.setText(""+MainActivity.CountZ3.getInt(MainActivity.zekr3,0));
id3Count.setText(""+MainActivity.CountZ4.getInt(MainActivity.zekr4,0));
id3Count.setText(""+MainActivity.CountZ5.getInt(MainActivity.zekr5,0));

}

下面的代码没有任何意义。

 @Override
public void onDetach() {
    super.onDetach();
    refreshF();
}

以上是关于空指针异常,android.widget.Tablayout.getChildAt(int)的主要内容,如果未能解决你的问题,请参考以下文章

java中的空指针异常怎么解决

SpringBoot配置拦截器空指针异常

java hashmap空指针异常

java的equals提示空指针异常

我的构造函数里面报空指针异常了!求大神帮忙

规避空指针异常