首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QuickContactBadge崩溃应用程序:

QuickContactBadge崩溃应用程序:
EN

Stack Overflow用户
提问于 2014-04-25 11:28:44
回答 1查看 101关注 0票数 0

我已经到处找过了,但没有一个解决方案对我的具体案例有效。如果我加上

代码语言:javascript
复制
QuickContactBadge contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);

我把它撞了。然后我把它改成:

代码语言:javascript
复制
    QuickContactBadge contactBadge;

    protected void onCreate(Bundle savedInstanceState) {

            ...

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

它也不会坠毁。但是如果我加上contactBadge.Anything();,它就会崩溃。不管采用什么方法,它都会崩溃。即contactBadge.assignContactFromEmail("foo@foo.com", true);

Android活动:

代码语言:javascript
复制
    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;
            }
        }

清单:

代码语言:javascript
复制
<?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>

布局:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-25 11:31:21

第一种方法:在实例化活动并初始化其成员变量时调用findViewById()。这个活动还没有一个Window,而且它也会有NPE。只在onCreate()或更高版本的setContentView()之后调用setContentView()

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

从后面添加的代码来看,视图似乎在片段布局中,而不是在活动布局中。它不会是onCreate()中的活动视图层次结构的一部分。您应该将代码移到片段的onCreateView()中:

代码语言:javascript
复制
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
contactBadge = (QuickContactBadge) rootView.findViewById(R.id.quickContactBadge);
contactBadge.assignContactFromEmail("pointlightproductions.net@gmail.com", true);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23291768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档