我已经到处找过了,但没有一个解决方案对我的具体案例有效。如果我加上
QuickContactBadge contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);我把它撞了。然后我把它改成:
QuickContactBadge contactBadge;
protected void onCreate(Bundle savedInstanceState) {
...
contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);
}它也不会坠毁。但是如果我加上contactBadge.Anything();,它就会崩溃。不管采用什么方法,它都会崩溃。即contactBadge.assignContactFromEmail("foo@foo.com", true);
Android活动:
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>发布于 2014-04-25 11:31:21
第一种方法:在实例化活动并初始化其成员变量时调用findViewById()。这个活动还没有一个Window,而且它也会有NPE。只在onCreate()或更高版本的setContentView()之后调用setContentView()。
第二种方法:您没有具有具有给定id视图的视图层次结构的setContentView()。将返回null并对其调用方法。
从后面添加的代码来看,视图似乎在片段布局中,而不是在活动布局中。它不会是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);https://stackoverflow.com/questions/23291768
复制相似问题