I am trying to open a dialog when clicking on a TextView. I have some code installed and I receive an error as shown below. Unfortunately I cannot resolve this on my own and thus need your help please.
The TextView
<TextView
android:id="@+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:fontFamily="casual"
android:gravity="center"
android:onClick="openOptions"
android:padding="4dp"
android:text="@string/optionsText"
android:textAllCaps="true"
android:textColor="#000"
android:textSize="14sp" />
MainActivity.java:
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView subtotal1, subtotal2, subtotal3, totalScore;
EditText[] editTexts = new EditText[30];
int[] allNumbers = new int[30];
int tempID, sumSub1, sumSub2, sumSub3, totalSum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subtotal1 = (TextView) findViewById(R.id.subtotal1);
subtotal2 = (TextView) findViewById(R.id.subtotal2);
subtotal3 = (TextView) findViewById(R.id.subtotal3);
totalScore = (TextView) findViewById(R.id.Total);
for (int i = 0; i < editTexts.length; i++) {
tempID = getResources().getIdentifier("field" + (i+1), "id", getPackageName());
editTexts[i] = (EditText) findViewById(tempID);
editTexts[i].addTextChangedListener(allTextWatcher);
}
}
public void openOptions() {
DialogFragment newFragment = new OptionsDialogFragment();
newFragment.show(getSupportFragmentManager(), "options");
}
}
OptionsDialogFragment.java:
import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
/**
* Created by lukas on 14.07.2017.
*/
public class OptionsDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_message)
.setItems(R.array.options_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
}
When clicking on the TextView, I get the following error message:
08-05 09:22:21.952 31036-31036/com.example.lukas.dicepokersheet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lukas.dicepokersheet, PID: 31036
Theme: themes:{default=overlay:com.cyngn.hexo, iconPack:com.cyngn.hexo, fontPkg:com.cyngn.hexo, com.android.systemui=overlay:com.cyngn.hexo, com.android.systemui.navbar=overlay:com.cyngn.hexo}
java.lang.IllegalStateException: Could not find method openOptions(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'Title'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)