Larigyn
Larigyn

Reputation: 115

Inflater error during Intent call

I have 3 java file MainActivity.java, AlertDialogUtil.java, and AddMoreDetails.java. In MainActivity.java a call to AlertDialogUtil.java an alert dialog will pop up there's an option button. When a button pressed an intent to a new activity AddMoreDetails.java will be call. Now when i try to press a button/any button Forced Closed has occur. I already read some answer's here but no luck at all. I'm just new learner in Android. I read more but still i can't make it work. Any help will do.

Here's my Code:

A call here MainActivity.java

alertDialog = AlertDialogUtil.whiteDialog(MainActivity.this);

in 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;
    }
}

in AddMoreDetails.java This class will call in pressing YES button.

My manifest file.

<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

Upvotes: 0

Views: 109

Answers (4)

Rohit5k2
Rohit5k2

Reputation: 18112

You already have the Context, why do you need to create a new variable with null value and use the null variable. No need.

Change

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

to

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

Update

I suspect this is package name issue.

Try changing package of class SquareImageView to com.example.junienegentein_screenshotapp

Upvotes: 0

You are getting a NPE because you are using the Intent constructor by passing a null as parameter..

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

instead use the context passed in the method param..

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();
            }
        })

Upvotes: 1

Siavash
Siavash

Reputation: 7853

You're creating your new intent with a null context instead of the activity context.

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

change mcontext to context, and delete Context mcontext = null;

Upvotes: 0

theapache64
theapache64

Reputation: 11744

I think it happens due to the NullPointerException

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

You should remove the below line from your code.

Context mcontext = null;

Upvotes: 0

Related Questions