Пожалуйста, я пытался обработать событие щелчка из моего проекта и на самом деле не нашел места, где можно было бы разместить оператор button.setOnClickListener (handler); . Правда в том, что у меня есть ViewPager внутри другого макета. xml файл, который я раздул из Fragment . Но у меня есть два других файла макета, которые я использовал для предоставления содержимого для самого ViewPager , что я и сделал, и он работал правильно. Но когда я попытался обработать событие onClick из любого из моих классов, он выдает исключение NullPointer . Это мой первый проект на Android, и я не могу найти никакого решения даже на StackOverflow, потому что ни один из ответов не решил мою проблему. Я буду очень признателен, если кто-нибудь сможет прочитать этот длинный код и дать ответ на мою проблему. Заранее спасибо. Это приложение-калькулятор
Код ниже
*In MainActivity.java*
package com.projects.calculatorapp2;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedState){
super.onCreate(savedState);
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, new ViewPagerFragment())
.commit();
}
}
*In ViewPagerFragment.java*
package com.projects.calculatorapp2;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
/**
* <b><p>Description</p></b>
* A simple {@link Fragment} subclass that provides view for the main activity.
* @author Noah
* @since 2020
*
*/
public class ViewPagerFragment extends Fragment implements View.OnClickListener {
EditText input;
protected ViewPager pager;
public StringBuilder builderText = new StringBuilder();
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_main, container, false);
pager = view.findViewById(R.id.pager);
input = view.findViewById(R.id.edit);
pager.setAdapter(new ButtonAdapter(getFragmentManager()));
return (view);
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedState){
Button one = view.findViewById(R.id.seven);
one.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int buttonId = view.getId();
switch (buttonId) {
case R.id.one: {
builderText.append(1);
Log.d(getClass().getSimpleName(), String.valueOf(builderText));
input.setText(builderText);
}
}
}
private class ButtonAdapter extends FragmentPagerAdapter {
// default constructor chained to superclass
ButtonAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 2; //number of pages
}
@Override
public Fragment getItem(int position) {
// Log.d(getClass().getSimpleName(), String.valueOf(position));
return (ButtonSet.newInstance(position));
}
}
}
*In ButtonSet.java*
package com.projects.calculatorapp2;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ButtonSet extends Fragment {
public static final String POS_KEY = "position";
static ButtonSet newInstance(int pos){
ButtonSet bs = new ButtonSet();
Bundle bundle = new Bundle();
bundle.putInt(POS_KEY, pos);
bs.setArguments(bundle);
return (bs);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container,
Bundle state) {
//noinspection ConstantConditions
if(getArguments().getInt(POS_KEY) == 0)
return inflater.inflate(R.layout.content_1,
container, false);
return inflater.inflate(R.layout.content_2,
container, false);
}
}
*In activity_main.xml* (Main layout file when app is lauched)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<EditText
android:id="@id/edit"
android:textIsSelectable="true"
android:layout_width="match_parent"
android:layout_height="200dp"
android:inputType="text"
android:textSize="40sp"
android:background="@android:color/transparent"
tools:ignore="Autofill,LabelFor"
android:hint="@string/ediText_hint"
android:gravity="center_vertical|end"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginEnd="110dp"
android:layout_marginRight="70dp"
android:background="@color/black"
android:clickable="false" />
<Button
android:id="@id/c_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:textColor="@color/white"
android:textSize="20sp"
android:text="@string/delete"
android:background="@color/dark_red"/>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
*In content_1.xml* (This is first content of ViewPager)
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.projects.calculatorapp2.ViewPagerFragment">
<!-- First button representing 7 -->
<Button
android:id="@id/seven"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/seven"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintWidth_percent="25"
app:layout_constraintEnd_toStartOf="@+id/eight"
app:layout_constraintRight_toLeftOf="@id/eight"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/four"
app:layout_constraintTop_toTopOf="parent" />
<!-- Second button representing 8 -->
<Button
android:id="@id/eight"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@id/nine"
app:layout_constraintBottom_toTopOf="@id/five"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/eight"/>
<!-- Third button representing 9 -->
<Button
android:id="@id/nine"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@id/divide"
app:layout_constraintBottom_toTopOf="@id/six"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/nine"/>
<!-- Fourth button representing division sign (/) -->
<Button
android:id="@id/divide"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/times"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/divide" />
<!-- Fifth button representing 4 -->
<Button
android:id="@id/four"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@id/seven"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/one"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/four" />
<!-- Sixth button representing 5 -->
<Button
android:id="@id/five"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@id/eight"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="@+id/four"
app:layout_constraintBottom_toTopOf="@id/two"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/five" />
<!-- Seventh button representing 6 -->
<Button
android:id="@id/six"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@+id/nine"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="@+id/five"
app:layout_constraintBottom_toTopOf="@id/three"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/six" />
<!-- Eight button representing multiplication sign (x) -->
<Button
android:id="@id/times"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@+id/divide"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="@+id/six"
app:layout_constraintBottom_toTopOf="@id/minus"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="normal"
android:text="@string/times" />
<!-- Eighth button representing 1 -->
<Button
android:id="@+id/one"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@+id/four"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@id/decimal"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/one" />
<!-- Ninth button representing 2 -->
<Button
android:id="@id/two"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@+id/five"
app:layout_constraintStart_toEndOf="@+id/one"
app:layout_constraintBottom_toTopOf="@id/zero"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/two" />
<!-- Tenth button representing 3 -->
<Button
android:id="@id/three"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@+id/times"
app:layout_constraintStart_toEndOf="@+id/two"
app:layout_constraintBottom_toTopOf="@id/equals"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/three" />
<!-- Eleventh button representing subtration sign ( - ) -->
<Button
android:id="@id/minus"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@+id/times"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="@+id/three"
app:layout_constraintBottom_toTopOf="@id/add"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/minus" />
<!-- Twelfth button representing decimal point ( . ) -->
<Button
android:id="@id/decimal"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@+id/one"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/decimal" />
<!-- Thirteenth button representing zero ( 0 )-->
<Button
android:id="@id/zero"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@+id/two"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="@+id/decimal"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/zero" />
<!-- Fourteenth button representing equals sign ( = ) -->
<Button
android:id="@id/equals"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@+id/three"
app:layout_constraintStart_toEndOf="@+id/zero"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/equals" />
<!-- Fifteenth button representing addition sign ( + ) -->
<Button
android:id="@id/add"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="@+id/minus"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="@string/add" />
</android.support.constraint.ConstraintLayout>
*In content_2.xml* (Second content of ViewPager)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Button
android:id="@id/sin"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/sin"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintBottom_toTopOf="@+id/log"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintWidth_percent="0.33"
/>
<Button
android:id="@id/cos"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/cos"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/sin"
app:layout_constraintEnd_toStartOf="@+id/tan"
app:layout_constraintBottom_toTopOf="@+id/log10"
app:layout_constraintWidth_percent="0.33"
/>
<Button
android:id="@id/tan"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/tan"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintBottom_toTopOf="@+id/factorial"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_percent="0.33"/>
<Button
android:id="@id/log"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/log"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintBottom_toTopOf="@+id/pi"
app:layout_constraintTop_toBottomOf="@id/sin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintWidth_percent="0.33"
/>
<Button
android:id="@id/log10"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/log10"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toBottomOf="@id/cos"
app:layout_constraintStart_toEndOf="@+id/log"
app:layout_constraintEnd_toStartOf="@+id/tan"
app:layout_constraintBottom_toTopOf="@+id/exp"
app:layout_constraintWidth_percent="0.33"
/>
<Button
android:id="@id/factorial"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/factorial"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toBottomOf="@id/tan"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_percent="0.33"
app:layout_constraintBottom_toTopOf="@+id/pow"/>
<Button
android:id="@id/pi"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/pi"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toBottomOf="@id/log"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintWidth_percent="0.33"
app:layout_constraintBottom_toBottomOf="parent"
/>
<Button
android:id="@id/exp"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/exp"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toBottomOf="@id/log10"
app:layout_constraintStart_toEndOf="@+id/pi"
app:layout_constraintEnd_toStartOf="@+id/tan"
app:layout_constraintWidth_percent="0.33"
app:layout_constraintBottom_toBottomOf="parent"
/>
<Button
android:id="@id/pow"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/pow"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toBottomOf="@id/factorial"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_percent="0.33"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
Сообщение журнала
Вызвано: java .lang. NullPointerException: попытка вызвать виртуальный метод void android .view.View.setOnClickListener (android .view.View $ OnClickListener) 'для ссылки на нулевой объект в com.projects.calculatorapp2.ViewPagerFragment.onViewCreated (ViewPagerFragment. java: 57)