java - 单击图像时不出现对话框 fragment

标签 java android xml dialog

我(认为)我已经配置了我的代码,这样当单击图像时,它会打开一个包含一些信息的对话框。但是,当我单击图像时,它没有打开对话框,而是显示设备屏幕上的 MY_APP 已停止

我正在使用 Android studio 来实现此目的,当我构建我的项目时,我没有遇到任何编译器错误。但是,它在该设备上不起作用。

这是我的代码: UserDataInputActivity.java

package com.example.owner.introductoryapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import android.view.View;


public class UserDataInputActivity extends AppCompatActivity
{
    String[] genderOptions = {"Male", "Female", "Other"};

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_data_input);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, genderOptions);
        //Find TextView control
        AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.GenderPromptValue);
        //Set the number of characters the user must type before the drop down list is shown
        acTextView.setThreshold(1);
        //Set the adapter
        acTextView.setAdapter(adapter);
    }

    public void onGenericMenuClick(View v)
    {
        Intent intent;

        if(v.getId() == R.id.pasttests)
        {
            intent =  new Intent(this, PastDiagnosticResult.class);
        }
        else if (v.getId() == R.id.currenttest)
        {
            intent =  new Intent(this, CurrentDiagnosticResultActivity.class);
        }
        else if (v.getId() == R.id.myinfo)
        {
            intent =  new Intent(this, UserDataInputActivity.class);
        }
        else //if(v.getId() == R.id.gaitInfoButton)
        {
            intent = new Intent(this, GaitInformation.class);
        }

        startActivity(intent);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}

GaitInformation.java

package com.example.owner.introductoryapplication;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class GaitInformation extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.GaitInformationContent)
                .setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        //GO BACK TO THE USER DATA INPUT PAGE - HOW?
                    }
                });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

LOGCAT CRUSH 报告

2019-01-04 14:24:34.741 8385-8385/com.example.owner.introductoryapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.owner.introductoryapplication, PID: 8385
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:6312)
        at android.view.View$PerformClick.run(View.java:24802)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:169)
        at android.app.ActivityThread.main(ActivityThread.java:6521)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:6312) 
        at android.view.View$PerformClick.run(View.java:24802) 
        at android.os.Handler.handleCallback(Handler.java:790) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:169) 
        at android.app.ActivityThread.main(ActivityThread.java:6521) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
     Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.owner.introductoryapplication/com.example.owner.introductoryapplication.GaitInformation}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1940)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1618)
        at android.app.Activity.startActivityForResult(Activity.java:4529)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
        at android.app.Activity.startActivityForResult(Activity.java:4487)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
        at android.app.Activity.startActivity(Activity.java:4848)
        at android.app.Activity.startActivity(Activity.java:4816)
        at com.example.owner.introductoryapplication.UserDataInputActivity.onGenericMenuClick(UserDataInputActivity.java:52)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:6312) 
        at android.view.View$PerformClick.run(View.java:24802) 
        at android.os.Handler.handleCallback(Handler.java:790) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:169) 
        at android.app.ActivityThread.main(ActivityThread.java:6521) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
2019-01-04 14:24:35.040 2367-2389/? E/libnav: CablComputeBacklightLevel(): UpdateType = DifferentSceneUpdate
2019-01-04 14:24:35.251 2638-2670/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.example.owner.introductoryapplication/com.example.owner.introductoryapplication.PastDiagnosticResult/1
2019-01-04 14:24:40.474 2367-2389/? E/libnav: CablComputeBacklightLevel(): UpdateType = DifferentSceneUpdate

我希望弹出该对话框,但相反,它说我的应用程序已停止工作。

最佳答案

您无法像这样打开 DialogFragment。请尝试这个。

package com.example.owner.introductoryapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import android.view.View;


public class UserDataInputActivity extends AppCompatActivity
{
    String[] genderOptions = {"Male", "Female", "Other"};

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_data_input);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, genderOptions);
        //Find TextView control
        AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.GenderPromptValue);
        //Set the number of characters the user must type before the drop down list is shown
        acTextView.setThreshold(1);
        //Set the adapter
        acTextView.setAdapter(adapter);
    }

    public void onGenericMenuClick(View v)
    {
        Intent intent = null;

        if(v.getId() == R.id.pasttests)
        {
            intent =  new Intent(this, PastDiagnosticResult.class);
        }
        else if (v.getId() == R.id.currenttest)
        {
            intent =  new Intent(this, CurrentDiagnosticResultActivity.class);
        }
        else if (v.getId() == R.id.myinfo)
        {
            intent =  new Intent(this, UserDataInputActivity.class);
        }
        else //if(v.getId() == R.id.gaitInfoButton)
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(UserDataInputActivity.this);
                builder.setMessage(R.string.GaitInformationContent)
                        .setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                //GO BACK TO THE USER DATA INPUT PAGE - HOW?
                            }
                        });
                // Create the AlertDialog object and return it
                builder.create().show();
        }

        if (intent != null) {
            startActivity(intent);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    }
}

关于java - 单击图像时不出现对话框 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54045814/

相关文章:

android - 如何修复索引 1 处的绑定(bind)值在 android 中为 null

java - 适配器泄漏内存?

java - 在一项 Activity 中添加两个 GalleryView

xml - XSD all 和 XSD sequence 之间的中间方式

java - Android TextToSpeech 其他库选项

java - 使 Java DateFormat 适用于较短的值

java - 如何从命令行运行包含 OpenCV 代码并使用 ant 创建的 .jar 文件?

java - 在 BlackBerry 上通过短信、电子邮件、推特、Facebook 分享

android - 自定义可扩展 RecyclerView 在 notifyItemRangeRemoved 和 notifyItemRangeInserted 上崩溃

c# - 从 XMLDocument 中的 xml 文件读取节点