I wrote the code for alert dialog for the event listener of the floating button but whenever i click on the floating button it always shows this error.
My MainActivity.java is as:
```package com.akashsoam.favlistapp;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.app.AlertDialog;
import android.text.InputType;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.akashsoam.favlistapp.databinding.ActivityMainBinding;
public class MainActivity extends Activity {
private ActivityMainBinding binding;
private RecyclerView categoryRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// setContentView(R.layout.activity_main);
categoryRecyclerView = findViewById(R.id.category_recyclerview);
categoryRecyclerView.setAdapter(new CategoryRecyclerAdapter());
categoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));
FloatingActionButton floatingActionButton = findViewById(R.id.fab);
floatingActionButton.setOnClickListener(view -> {
displayCreateCategoryDialog();
});
}
private void displayCreateCategoryDialog() {
String alertTitle = getString(R.string.create_category);
String positiveButtonTitle = getString(R.string.positive_button_title);
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
EditText categoryEditText = new EditText(this);
categoryEditText.setInputType(InputType.TYPE_CLASS_TEXT);
// TODO:debug below code it is showing errors
builder.setTitle(alertTitle).setPositiveButton(positiveButtonTitle, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).setView(categoryEditText).create().show();
}
} ```
AndroidManifest.xml ```
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.AppCompat.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>```
**The error always popus up I have tried many solutions to the similar questions already asked before on stackoverflow but the don't work **
Like
I tried to give it a non Appcompat theme- that doesn't work
I tried extending MainActivity with Activity instead of AppCompatActivity but that doesn't work either. Please review the code by running it in your android studio.
This line is the issue for without even testing the code,you should use your activity context instead as so: