0

我四处搜索,但没有一个解决方案适用于我的具体情况。如果我添加

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

我它崩溃了。然后我将其更改为:

    QuickContactBadge contactBadge;

    protected void onCreate(Bundle savedInstanceState) {

            ...

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

它不会崩溃。但是如果我添加contactBadge.Anything();它会崩溃。不管用什么方法都会崩溃。IEcontactBadge.assignContactFromEmail("foo@foo.com", true);

安卓活动:

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

显现:

<?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>

布局:

<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>
4

1 回答 1

2

第一种方法:您在findViewById()实例化活动并初始化其成员变量时调用。该活动还Window没有,它将是NPE。findViewById()仅在.onCreate()之后或之后调用setContentView()

第二种方法:您没有具有setContentView()给定 id 的视图的视图层次结构。null返回并在其上调用方法将 NPE。

从您后来添加的代码来看,视图似乎在您的片段布局中,而不是在活动布局中。它不会成为onCreate(). 您应该将代码移动到片段中onCreateView()

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
contactBadge = (QuickContactBadge) rootView.findViewById(R.id.quickContactBadge);
contactBadge.assignContactFromEmail("pointlightproductions.net@gmail.com", true);
于 2014-04-25T11:31:21.107 回答