Суть программы заключается во вставлении текста в поля через нажатие кнопки возле поля. Текст для вставление берется с EditText
сверху.
Все как бы просто, но при нажатии кнопки программа просто вылетает с вот такой ошибкой в логе: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
. При поиска я нашел информацию, что эта ошибка появляется из-за того, что я пытаюсь вызвать метод из активити, которая не используется, но я не могу понять что это за активити.
Main Activity.java:
public class MainActivity extends FragmentActivity {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 6;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager2 viewPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private FragmentStateAdapter pagerAdapter;
TextView dayText;
TextView text9;
TextView text10;
TextView text11;
TextView text12;
TextView text13;
TextView text14;
TextView text15;
TextView text16;
TextView text17;
TextView text18;
TextView text19;
EditText eText;
Methods methods;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
// Instantiate a ViewPager2 and a PagerAdapter.
viewPager = findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(this);
viewPager.setAdapter(pagerAdapter);
dayText = (TextView) findViewById(R.id.day);
eText = (EditText) findViewById(R.id.eText);
text9 = (TextView) findViewById(R.id.name9text);
text10 = (TextView) findViewById(R.id.name10text);
text11 = (TextView) findViewById(R.id.name11text);
text12 = (TextView) findViewById(R.id.name12text);
text13 = (TextView) findViewById(R.id.name13text);
text14 = (TextView) findViewById(R.id.name14text);
text15 = (TextView) findViewById(R.id.name15text);
text16 = (TextView) findViewById(R.id.name16text);
text17 = (TextView) findViewById(R.id.name17text);
text18 = (TextView) findViewById(R.id.name18text);
text19 = (TextView) findViewById(R.id.name19text);
methods = new Methods();
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
methods.setDay(position, dayText);
}
@Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
}
public void addText(View v){
switch (v.getId()){ //Вот при нажатии какой-либо из таких кнопок программа и вылетает(
case R.id.addText9:
text9.setText(eText.getText().toString());
break;
case R.id.addText10:
text10.setText(eText.getText().toString());
break;
case R.id.addText11:
text11.setText(eText.getText().toString());
break;
case R.id.addText12:
text12.setText(eText.getText().toString());
break;
case R.id.addText13:
text13.setText(eText.getText().toString());
break;
case R.id.addText14:
text14.setText(eText.getText().toString());
break;
case R.id.addText15:
text15.setText(eText.getText().toString());
break;
case R.id.addText16:
text16.setText(eText.getText().toString());
break;
case R.id.addText17:
text17.setText(eText.getText().toString());
break;
case R.id.addText18:
text18.setText(eText.getText().toString());
break;
case R.id.addText19:
text19.setText(eText.getText().toString());
break;
}
}
public void delText(View v){
switch (v.getId()){
case R.id.delText9:
text9.setText("");
break;
case R.id.delText10:
text10.setText("");
break;
case R.id.delText11:
text11.setText("");
break;
case R.id.delText12:
text12.setText("");
break;
case R.id.delText13:
text13.setText("");
break;
case R.id.delText14:
text14.setText("");
break;
case R.id.delText15:
text15.setText("");
break;
case R.id.delText16:
text16.setText("");
break;
case R.id.delText17:
text17.setText("");
break;
case R.id.delText18:
text18.setText("");
break;
case R.id.delText19:
text19.setText("");
break;
}
}
@Override
public void onBackPressed() {
if (viewPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStateAdapter {
public ScreenSlidePagerAdapter(FragmentActivity fa) {
super(fa);
}
@Override
public Fragment createFragment(int position) {
return new fragment_screen_slide_page();
}
@Override
public int getItemCount() {
return NUM_PAGES;
}
}
}
Methods.java:
public class Methods {
public void setDay(int position, TextView dayText){
switch (position){
case 0:
dayText.setText("Monday");
break;
case 1:
dayText.setText("Tuesday");
break;
case 2:
dayText.setText("Wednesday");
break;
case 3:
dayText.setText("Thursday");
break;
case 4:
dayText.setText("Friday");
break;
case 5:
dayText.setText("Saturday");
break;
}
}
}
В этом классе пока есть только один метод, но он дополнялся бы. Этот класс не имеет отношения к ошибке и был добавлен в вопрос только для полноты картины.
activity_screen_slide.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">
<TextView
android:id="@+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Day"
android:gravity="center"
android:background="@color/white"></TextView>
<EditText
android:id="@+id/eText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:background="@color/white"></EditText>
<androidx.viewpager2.widget.ViewPager2
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" /></LinearLayout>
activity_screen_slide_page.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="@color/white"
android:id="@+id/ll">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name9"
android:layout_weight="9">
<TextView
android:id="@+id/t9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="09:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:id="@+id/name9text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:gravity=""
android:textSize="20sp"></TextView>
<ImageButton
android:id="@+id/addText9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name10"
android:layout_weight="9">
<TextView
android:id="@+id/t10"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="10:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:id="@+id/name10text"
android:textSize="20sp"
android:gravity=""></TextView>
<ImageButton
android:id="@+id/addText10"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText10"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name11"
android:layout_weight="9">
<TextView
android:id="@+id/t11"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="11:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:id="@+id/name11text"
android:textSize="20sp"
android:gravity=""></TextView>
<ImageButton
android:id="@+id/addText11"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText11"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name12"
android:layout_weight="9">
<TextView
android:id="@+id/t12"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="12:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:id="@+id/name12text"
android:textSize="20sp"
android:gravity=""></TextView>
<ImageButton
android:id="@+id/addText12"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText12"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name13"
android:layout_weight="9">
<TextView
android:id="@+id/t13"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="13:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:id="@+id/name13text"
android:textSize="20sp"
android:gravity=""></TextView>
<ImageButton
android:id="@+id/addText13"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText13"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name14"
android:layout_weight="9">
<TextView
android:id="@+id/t14"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="14:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:id="@+id/name14text"
android:textSize="20sp"
android:gravity=""></TextView>
<ImageButton
android:id="@+id/addText14"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText14"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name15"
android:layout_weight="9">
<TextView
android:id="@+id/t15"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="15:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:id="@+id/name15text"
android:textSize="20sp"
android:gravity=""></TextView>
<ImageButton
android:id="@+id/addText15"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText15"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name16"
android:layout_weight="9">
<TextView
android:id="@+id/t16"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="16:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:id="@+id/name16text"
android:textSize="20sp"
android:gravity=""></TextView>
<ImageButton
android:id="@+id/addText16"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText16"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name17"
android:layout_weight="9">
<TextView
android:id="@+id/t17"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="17:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:id="@+id/name17text"
android:textSize="20sp"
android:gravity=""></TextView>
<ImageButton
android:id="@+id/addText17"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText17"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name18"
android:layout_weight="9">
<TextView
android:id="@+id/t18"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="18:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:id="@+id/name18text"
android:textSize="20sp"
android:gravity=""></TextView>
<ImageButton
android:id="@+id/addText18"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText18"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/name19"
android:layout_weight="9">
<TextView
android:id="@+id/t19"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="19:00"
android:textSize="20sp"
android:gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10000"
android:id="@+id/name19text"
android:textSize="20sp"
android:gravity=""></TextView>
<ImageButton
android:id="@+id/addText19"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="addText"
android:src="@android:drawable/ic_input_add"
android:text="Add"
android:textAllCaps="false"></ImageButton>
<ImageButton
android:id="@+id/delText19"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/white"
android:onClick="delText"
android:src="@android:drawable/ic_delete"
android:text="Del"
android:textAllCaps="false"></ImageButton>
</LinearLayout></LinearLayout>
Здесь много повторяющегося кода, ибо это почти таблица
Надеюсь на вашу помощь.
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
Программа выдает null pointer exception когда я пытаюсь вывести n, то есть она не находит письма в папке INBOX, хотя они там есть
Приветствую и благодарю за проявленный интерес к вопросу
https://webdesign-masterru/blog/tools/2016-06-04-sass