Intent调用期间出现Inflater错误

时间:2016-02-03 19:29:51

标签: java android android-intent alertdialog

我有3个java文件MainActivity.javaAlertDialogUtil.javaAddMoreDetails.java。在MainActivity.java中,对AlertDialogUtil.java的调用会弹出一个警告对话框,其中有一个选项按钮。当按下按钮意图新活动AddMoreDetails.java时,将调用该按钮。现在当我尝试按下按钮/任何按钮强制关闭已经发生。我已经在这里读了一些答案,但根本没有运气。我只是Android的新学习者。我读了更多但仍然无法使它工作。任何帮助都可以。

这是我的代码:

在此致电MainActivity.java

alertDialog = AlertDialogUtil.whiteDialog(MainActivity.this);
AlertDialogUtil.java

中的

public class AlertDialogUtil extends DialogFragment{
public static AlertDialog whiteDialog(final Context context){
        AlertDialog.Builder builder = new AlertDialog.Builder(context,
                AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        builder.setTitle("Add more Details")
        .setIcon(R.drawable.grm_edit)
        .setCancelable(false)
        .setMessage("We would like you to add more details.")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            //@Override
            public void onClick(DialogInterface dialog, int arg1) {
                //Toast toast = Toast.makeText(context, "You press YES:)", Toast.LENGTH_LONG);
                //toast.show();
                Context mcontext = null;
                Intent myIntent = new Intent(mcontext, AddMoreDetails.class);
                mcontext.startActivity(myIntent);    
                dialog.cancel();
            }
        })
        .setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        return alertDialog;
    }
}
AddMoreDetails.java中的

此课程将按“是”按钮调用。

我的清单文件。

<activity
            android:name=".AddMoreDetails"
            android:label="@string/more_details"
            android:parentActivityName=".MainActivity"/>

        <activity
            android:name="com.example.junienegentein_screenshotapp.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

logcat的

02-04 03:49:12.859: E/AndroidRuntime(22981): FATAL EXCEPTION: main
02-04 03:49:12.859: E/AndroidRuntime(22981): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.junienegentein_screenshotapp/com.example.junienegentein_screenshotapp.AddMoreDetails}: android.view.InflateException: Binary XML file line #60: Error inflating class com.example.junienegentien_form.SquareImageView
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.os.Handler.dispatchMessage(Handler.java:107)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.os.Looper.loop(Looper.java:194)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.app.ActivityThread.main(ActivityThread.java:5371)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at java.lang.reflect.Method.invoke(Method.java:525)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at dalvik.system.NativeStart.main(Native Method)
02-04 03:49:12.859: E/AndroidRuntime(22981): Caused by: android.view.InflateException: Binary XML file line #60: Error inflating class com.example.junienegentien_form.SquareImageView
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at com.example.junienegentein_screenshotapp.AddMoreDetails.onCreate(AddMoreDetails.java:36)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.app.Activity.performCreate(Activity.java:5135)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
02-04 03:49:12.859: E/AndroidRuntime(22981):    ... 11 more
02-04 03:49:12.859: E/AndroidRuntime(22981): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.junienegentien_form.SquareImageView" on path: DexPathList[[zip file "/data/app/com.example.junienegentein_screenshotapp-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.junienegentein_screenshotapp-1, /vendor/lib, /system/lib]]
02-04 03:49:12.859: E/AndroidRuntime(22981):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
02-04 03:49:12.859: E/AndroidRuntime(22981):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
02-04 03:49:12.859: E/AndroidRuntime(22981):    ... 22 more

4 个答案:

答案 0 :(得分:1)

您正在获取NPE,因为您通过传递null作为参数来使用Intent构造函数。

   ....
     //toast.show();
     Context mcontext = null;
     Intent myIntent = new Intent(mcontext, AddMoreDetails.class);

改为使用方法参数中传递的上下文。

public static AlertDialog whiteDialog(final Context context){
        Context mcontext = context;  //<-- HERE!!
        AlertDialog.Builder builder = new AlertDialog.Builder(context,
                AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        builder.setTitle("Add more Details")
        .setIcon(R.drawable.grm_edit)
        .setCancelable(false)
        .setMessage("We would like you to add more details.")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            //@Override
            public void onClick(DialogInterface dialog, int arg1) {
                //Toast toast = Toast.makeText(context, "You press YES:)", Toast.LENGTH_LONG);
                //toast.show();
                //Context mcontext = null;  //moved up!
                Intent myIntent = new Intent(mcontext, AddMoreDetails.class);
                mcontext.startActivity(myIntent);    
                dialog.cancel();
            }
        })

答案 1 :(得分:0)

我认为这是由NullPointerException

引起的
Context mcontext = null;
Intent myIntent = new Intent(mcontext, AddMoreDetails.class);

您应该从代码中删除以下行。

Context mcontext = null;

答案 2 :(得分:0)

您使用空上下文而不是活动上下文创建新意图。

Context mcontext = null;
Intent myIntent = new Intent(mcontext, AddMoreDetails.class);

mcontext更改为context,然后删除Context mcontext = null;

答案 3 :(得分:0)

您已经拥有Context,为什么需要使用null值创建一个新变量并使用null变量。没必要。

更改

Context mcontext = null;
Intent myIntent = new Intent(mcontext, AddMoreDetails.class);

Intent myIntent = new Intent(context, AddMoreDetails.class); // use context from method parameter

<强>更新

我怀疑这是包名称问题。

尝试将班级SquareImageView更改为com.example.junienegentein_screenshotapp