java - 从对话框开始新的活动
问题描述
按下 OK 按钮后,我尝试打开一个新的 Activity,但它不起作用。这是我的代码:
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);
}
}
我试图找出问题所在,但我的问题没有答案。
解决方案
我用你的代码创建了一个演示,效果很好。你可以检查我的代码如下。
我在模拟器Android 10上测试过
主要活动:
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);
}
}
主要的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>
类 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);
}
}
布局文件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>
类 NewList
public class NewList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_list_class);
}
}
该文件activity_new_list_class
仅包含一个 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>
清单文件:
<?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>
关于崩溃或某事。像这样,您可以在 Android Studio 底部的TabRun
或 Tab上查看 logcat 。Logcat
推荐阅读
- prolog - 在 SICStus Prolog 中使用 CLMUL
- azure-media-player - 防止在 Azure 媒体播放器上跳过视频
- kubernetes - `helm reset --force` 是每个物理主机执行一次还是每个集群执行一次?
- android - Flutter 本地通知导致崩溃,仅在 APK 版本中
- c - 线程处理
- javascript - 遍历文件夹并导入 index.js
- python - Keras 中的发散损失与自定义损失
- java - 如何处理 RestControllerAdvice 中抛出的异常?
- javascript - 在哪里放置`axios.interceptor`?在 react-create-app 的单独服务中?
- laravel - 从关系表中提取数据到 AppServiceProvider