0

I have searched around but none of the solutions worked for my specific case. If I add

QuickContactBadge contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);

I it crashes. Then I changed it to:

    QuickContactBadge contactBadge;

    protected void onCreate(Bundle savedInstanceState) {

            ...

            contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);
    }

And it doesn't crash. But if I add contactBadge.Anything(); it crashes. No matter what method it crashes. I.E. contactBadge.assignContactFromEmail("foo@foo.com", true);

Android Activity:

    public class MainActivity extends Activity {

            QuickContactBadge contactBadge;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_main);

                if (savedInstanceState == null) {

                    getFragmentManager().beginTransaction().add(R.id.container, new MainFragment()).commit();
                }

                contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);
                contactBadge.assignContactFromEmail("pointlightproductions.net@gmail.com", true);
            }

            public static class MainFragment extends Fragment {

            public MainFragment() {

            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                View rootView = inflater.inflate(R.layout.fragment_main, container, false);

                return rootView;
            }
        }

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pointlight.android.kingdomcraft"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/android:Theme.Holo.NoActionBar" >
        <activity
            android:name="com.pointlight.android.kingdomcraft.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.pointlight.android.kingdomcraft.MainActivity$MainFragment" >


    <QuickContactBadge
        android:id="@+id/quickContactBadge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
SemperAmbroscus
  • 1,308
  • 2
  • 12
  • 23

1 Answers1

2

First method: You're calling findViewById() when the activity is instantiated and its member variables are initialized. The activity won't have a Window yet and it will NPE. Call findViewById() only in onCreate() or later, after setContentView().

Second method: You don't have setContentView() with a view hierarchy that has a view with the given id. null is returned and invoking method on it will NPE.

From the code you later added, it seems the view is in your fragment layout and not the activity layout. It won't be a part of the activity view hierarchy in onCreate(). You should move the code to the fragment's onCreateView() instead:

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
contactBadge = (QuickContactBadge) rootView.findViewById(R.id.quickContactBadge);
contactBadge.assignContactFromEmail("pointlightproductions.net@gmail.com", true);
laalto
  • 150,114
  • 66
  • 286
  • 303
  • I have setContentView it's for the most part the default generated activity, should I post my class? – SemperAmbroscus Apr 25 '14 at 11:36
  • Generally, yes, exception stacktrace, layout name used in `setContentView()` and the corresponding layout XML are relevant in further diagnosis. – laalto Apr 25 '14 at 11:38
  • well I didn't wan't to get critisized about proper question format, besides I am trying what Aksh posted – SemperAmbroscus Apr 25 '14 at 11:42
  • I fixed it, don't know why people here are so crabbby, post to much code get critisized, too little get critisized and still have to post more, in the end i still got down voted – SemperAmbroscus Apr 25 '14 at 11:49
  • I would rather not have it on my activity layout because then it could be viewed from every fragment/page – SemperAmbroscus Apr 25 '14 at 12:00