我的应用程序中有3个按钮,我的选项卡活动的每个选项卡上都有一个按钮,我希望每个按钮都执行相同的操作。但是,单击其中一个按钮时只有一个按钮可以正常工作。它与其他两个崩溃了。
public void changeOperation (View view) {
Button b1, b2, b3;
b1 = findViewById(R.id.prac_op); // Line 116 (As mentioned in exception)
b2 = findViewById(R.id.operation);
b3 = findViewById(R.id.instruc_op);
switch (operation) {
case 0:
operation = 1;
b1.setText("-");
b2.setText("-");
b3.setText("-");
break;
case 1:
operation = 2;
b1.setText("x");
b2.setText("x");
b3.setText("x");
break;
case 2:
operation = 3;
b1.setText("\u00F7");
b2.setText("\u00F7");
b3.setText("\u00F7");
break;
default:
case 3:
operation = 0;
b1.setText("+");
b2.setText("+");
b3.setText("+");
break;
}
Tab3Practice.reroll();
}
例外:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.orion31.mathmachine, PID: 24662
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6254)
at android.view.View$PerformClick.run(View.java:24705)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6600)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:772)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6254)
at android.view.View$PerformClick.run(View.java:24705)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6600)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:772)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.orion31.mathmachine.MathMain.changeOperation(MathMain.java:116)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6254)
at android.view.View$PerformClick.run(View.java:24705)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6600)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:772)
此功能用作3个按钮的OnClickListener
,但只有第二个标签实际上成功调用此功能的按钮;点击时另外两个崩溃。我已经对一个OnClickListener
上有多个按钮进行了一些研究。这是我的代码的问题,还是对Android的限制?
**编辑:** MathMain.java
public class MathMain extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
public static int operation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_math_main);
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 = findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1);
TabLayout tabLayout = findViewById(R.id.tabDots);
tabLayout.setupWithViewPager(mViewPager, true);
}
public void changeOperation (View view) {
Button b1, b2, b3;
b1 = findViewById(R.id.prac_op); // Line 116 (As mentioned in exception)
b2 = findViewById(R.id.operation);
b3 = findViewById(R.id.instruc_op);
switch (operation) {
case 0:
operation = 1;
b1.setText("-");
b2.setText("-");
b3.setText("-");
break;
case 1:
operation = 2;
b1.setText("x");
b2.setText("x");
b3.setText("x");
break;
case 2:
operation = 3;
b1.setText("\u00F7");
b2.setText("\u00F7");
b3.setText("\u00F7");
break;
default:
case 3:
operation = 0;
b1.setText("+");
b2.setText("+");
b3.setText("+");
break;
}
Tab3Practice.reroll();
}
public void checkAnswer (View view) {
TextView num1input = findViewById(R.id.num1);
TextView num2input = findViewById(R.id.num2);
TextView num3guess = findViewById(R.id.guessAnswer);
if (
(num1input.getText() == null || num1input.getText().equals("")) ||
(num2input.getText() == null || num2input.getText().equals("")) ||
(num3guess.getText() == null || num3guess.getText().equals(""))) return;
double num1 = Double.parseDouble(num1input.getText().toString());
double num2 = Double.parseDouble(num2input.getText().toString());
double num3 = Double.parseDouble(num3guess.getText().toString());
CheckDialog cd = new CheckDialog();
double answer;
switch (operation) {
default:
case 0:
answer = num1 + num2;
break;
case 1:
answer = num1 - num2;
break;
case 2:
answer = num1 * num2;
break;
case 3:
answer = num1 / num2;
break;
}
if (answer == num3) {
cd.setMessage("Good Job! That's Correct.");
} else if (Math.abs(answer - num3) <= 10) {
cd.setMessage("Very close! Keep trying!");
} else {
cd.setMessage("Nope! That's not it. Try again.");
}
num1input.setText("");
num2input.setText("");
num3guess.setText("");
FragmentManager ft = getSupportFragmentManager();
cd.show(ft, "check_dialog");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_math_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_about) {
startActivity(new Intent(this, AboutScreen.class));
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public static class CheckDialog extends DialogFragment {
String message;
@Override
public Dialog onCreateDialog (Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()).setTitle("Check Your Work").setMessage(message)
.setNegativeButton("CLOSE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
@Override
public void onStart() {
super.onStart();
((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);
}
public void setMessage(String message) {
this.message = message;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new Tab1Check();
case 1:
return new Tab2Main();
case 2:
return new Tab3Practice();
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}
答案 0 :(得分:0)
在每个片段中引用onCreateView中的按钮
public class YourFragment extends Fragment ...
Button button1;
public void onCreateView(.... )
{
View view = inflate ...
button1 = view.findViewById(R.id.prac_opl);
}
在每个片段中添加此方法
public void updateText(String text){
button1.setText(text);
}
修改您的活动
public class MathMain extends AppCompatActivity {
Tab1Check fragmentTab1;
Tab2Main fragmentTab2;
Tab3Practice fragmentTab3;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
public static int operation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_math_main);
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.
fragmentTab1 = new Tab1Check();
fragmentTab3 = new Tab2Main();
fragmentTab3 = new Tab3Practice();
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1);
TabLayout tabLayout = findViewById(R.id.tabDots);
tabLayout.setupWithViewPager(mViewPager, true);
}
public void checkAnswer(View view) {
TextView num1input = findViewById(R.id.num1);
TextView num2input = findViewById(R.id.num2);
TextView num3guess = findViewById(R.id.guessAnswer);
if (
(num1input.getText() == null || num1input.getText().equals("")) ||
(num2input.getText() == null || num2input.getText().equals("")) ||
(num3guess.getText() == null || num3guess.getText().equals(""))) return;
double num1 = Double.parseDouble(num1input.getText().toString());
double num2 = Double.parseDouble(num2input.getText().toString());
double num3 = Double.parseDouble(num3guess.getText().toString());
CheckDialog cd = new CheckDialog();
double answer;
switch (operation) {
default:
case 0:
answer = num1 + num2;
break;
case 1:
answer = num1 - num2;
break;
case 2:
answer = num1 * num2;
break;
case 3:
answer = num1 / num2;
break;
}
if (answer == num3) {
cd.setMessage("Good Job! That's Correct.");
} else if (Math.abs(answer - num3) <= 10) {
cd.setMessage("Very close! Keep trying!");
} else {
cd.setMessage("Nope! That's not it. Try again.");
}
num1input.setText("");
num2input.setText("");
num3guess.setText("");
FragmentManager ft = getSupportFragmentManager();
cd.show(ft, "check_dialog");
}
public void changeOperation(View view) {
switch(MathMain.operation)
{
case 0:
MathMain.operation = 1;
fragmentTab1.updateText("-");
fragmentTab2.updateText("-");
fragmentTab3.updateText("-");
break;
case 1:
MathMain.operation = 2;
fragmentTab1.updateText("x");
fragmentTab2.updateText("x");
fragmentTab3.updateText("x");
break;
case 2:
MathMain.operation = 3;
fragmentTab1.updateText("\u00F7");
fragmentTab2.updateText("\u00F7");
fragmentTab3.updateText("\u00F7");
break;
default:
case 3:
MathMain.operation = 0;
fragmentTab1.updateText("+");
fragmentTab2.updateText("+");
fragmentTab3.updateText("+");
break;
}
reroll();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_math_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_about) {
startActivity(new Intent(this, AboutScreen.class));
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public static class CheckDialog extends DialogFragment {
String message;
@Override
public Dialog onCreateDialog (Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()).setTitle("Check Your Work").setMessage(message)
.setNegativeButton("CLOSE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
@Override
public void onStart() {
super.onStart();
((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);
}
public void setMessage(String message) {
this.message = message;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return fragmentTab1;
case 1:
return fragmentTab2;
case 2:
return fragmentTab3;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}