I try to open a new Activity after pressing OK Button but it doesn't work. Here is my code:
public class NameOfNewList extends AppCompatDialogFragment {
Context mActivity;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater = requireActivity().getLayoutInflater();
mActivity = getContext();
builder.setTitle("Nowa lista")
.setView(layoutInflater.inflate(R.layout.activity_new_list_dialog, null))
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startNewList();
}
});
return builder.create();
}
public void startNewList() {
Intent intent = new Intent(mActivity, NewList.class);
startActivity(intent);
}
}
I've tried to find what is the problem but there was no answers to my question.
CodePudding user response:
You just need to add context
public void startNewList() {
Intent intent = new Intent(mActivity, NewList.class);
mActivity.startActivity(intent);
}
CodePudding user response:
I used your code to create a demo, it worked fine. You can check my code as below.
I tested on emulator Android 10
The main Activity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startDialogFragment();
}
});
}
private void startDialogFragment() {
new NameOfNewList().show(getSupportFragmentManager(), NameOfNewList.TAG);
}
}
The main xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<Button
android:id="@ id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The class NameOfNewList
public class NameOfNewList extends AppCompatDialogFragment {
Context mActivity;
public static String TAG = "NameOfNewList";
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
LayoutInflater layoutInflater = requireActivity().getLayoutInflater();
mActivity = getContext();
builder.setTitle("Nowa lista")
.setView(layoutInflater.inflate(R.layout.activity_new_list_dialog, null))
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startNewList();
}
});
return builder.create();
}
public void startNewList() {
Intent intent = new Intent(mActivity, NewList.class);
startActivity(intent);
}
}
The layout file activity_new_list_dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".dialog.NewList">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TANCOLO" />
</LinearLayout>
The class NewList
public class NewList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_list_class);
}
}
The file activity_new_list_class
just contains one TextView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".dialog.NewList">
<TextView
android:id="@ id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World 1111!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.teststackoverflow">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TestStackoverflow">
<activity android:name=".dialog.NewList"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Regards the crash or sth. like that, you can check the logcat on Tab Run
or Tab Logcat
in the bottom of Android Studio.