【问题标题】:Dialog Fragment does not appear when image is clicked单击图像时不出现对话框片段
【发布时间】:2019-05-31 10:20:59
【问题描述】:

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

我为此使用 Android Studio,当我 build 我的项目时,我没有收到任何编译器错误。但是,它在设备上不起作用。

这是我的代码: 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 REPORT

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

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

【问题讨论】:

  • 当你点击图片时,会在运行时发生暗恋。你能发布暗恋报告吗?你会在 logcat 中找到它。
  • @ZoarderAlMuktadir 我已经指定了暗恋报告!

标签: java android xml dialog


【解决方案1】:

在你的课堂上 GaitInfomation 上线 新的 AlertDialog.Builder(getActivity()); 你必须传递有效的上下文,我怀疑它是 null 查看代码。

更新:而不是启动一个新的活动

      else //if(v.getId() == R.id.gaitInfoButton) {
          AlertDialog.Builder(this)
                  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?
                    return;
                    }
               });
                 .create()
                .show()
           }

【讨论】:

  • 如何传递“有效上下文”?您能否提供一个代码 sn-p 以便我理解您的意思?谢谢。
  • Knivasara 请参阅imgur.com/7wploeS - 它显示了我的代码在我的 IDE 上的样子。至于我运行它的时候,我在很多地方得到error: &lt;identifier&gt; expectederror: ';' expectederror: illegal start of expression
【解决方案2】:

改变这个

intent = new Intent(this, GaitInformation.class);

进入这个

FragmentManager manager = getSupportFragmentManager();
GaitInformation dialog = new GaitInformation();
dialog.show(manager, "dialog");

【讨论】:

  • 这似乎不起作用。错误输出为:error: variable intent might not have been initialized。这是有道理的,因为最后一个 else 条件没有初始化 Intent,而不是上面的另一个条件。
  • 试试这个启动intent Intent intent = null;
  • 它有效;谢谢!你能告诉我上面的代码是怎么回事吗?
  • 在你的 if else 块默认行为没有初始化意图。只是为了防止该错误,我们传递了 null。所以它在那之后被初始化了,但只是值是空的。
【解决方案3】:

您不能像这样打开 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);
        }
    }
}

【讨论】:

  • Al Maktadir 我得到相同的错误输出:error: variable intent might not have been initialized。我怎样才能解决这个问题?用户关闭Dialog后,应该回到UserDataInputActivity的屏幕...
  • 它有效;谢谢。你能回顾一下你改变了什么吗?
  • 只需将intent对象初始化为null即可。
  • 好的,明白了。 @Zoarder Al Muktadir 您能否也看看这些问题,我也遇到了很多麻烦。我相信这是一个简单的解决方案...stackoverflow.com/questions/54036101/…stackoverflow.com/questions/53983231/…。最后,点赞就好了! :)
  • 好的,让我检查一下...谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-02
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多