热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:空指针异常,android.widget.Tablayout.getChildAt(int)

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

篇首语:本文由编程笔记#小编为大家整理,主要介绍了空指针异常,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 mFragmentList = new ArrayList<>();
private final List 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();
}


推荐阅读
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 在springmvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?Controller方法 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • Java如何导入和导出Excel文件的方法和步骤详解
    本文详细介绍了在SpringBoot中使用Java导入和导出Excel文件的方法和步骤,包括添加操作Excel的依赖、自定义注解等。文章还提供了示例代码,并将代码上传至GitHub供访问。 ... [详细]
author-avatar
cb
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有