QuickContactBadge вылетает приложение:
Я искал вокруг, но ни одно из решений не сработало для моего конкретного случая. Если я добавлю
QuickContactBadge contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);
Я вылетает. Затем я изменил это на:
QuickContactBadge contactBadge;
protected void onCreate(Bundle savedInstanceState) {
...
contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);
}
И это не терпит крах. Но если я добавлю contactBadge.Anything();
это падает. Неважно, каким методом он вылетает. IE 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;
}
}
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>
Планировка:
<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>
1 ответ
Первый метод: вы звоните findViewById()
когда действие создается и переменные-члены инициализируются. Деятельность не будет иметь Window
пока что и будет NPE. Вызов findViewById()
только в onCreate()
или позже, после setContentView()
,
Второй метод: у вас нет setContentView()
с иерархией представления, которая имеет представление с заданным идентификатором. 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);