Can anyone please tell me why this Activity crashes on load?
I tried to comment out the part of code that creates a child view from the root view, and it worked, but when I put it back, the activity crashes.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class NumbersActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
//Create ArrayList of words
ArrayList<String> words = new ArrayList<>();
words.add("one");
words.add("Two");
words.add("Three");
words.add("Four");
words.add("Five");
words.add("Six");
words.add("Seven");
words.add("Eight");
words.add("Nine");
words.add("Ten");
LinearLayout rootView = findViewById(R.id.rootView);
TextView wordView = new TextView(this);
rootView.addView(wordView);
wordView.setText(words.get(1));
}
}
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/tan_background"
android:orientation="vertical"
tools:context="com.example.android.miwok_startercode301.MainActivity"
android:id="@+id/rootView">
<TextView
android:id="@+id/numbers"
style="@style/CategoryStyle"
android:background="@color/category_numbers"
android:text="@string/category_numbers"
/>
<TextView
android:id="@+id/family"
style="@style/CategoryStyle"
android:background="@color/category_family"
android:text="@string/category_family"
/>
<TextView
android:id="@+id/colors"
style="@style/CategoryStyle"
android:background="@color/category_colors"
android:text="@string/category_colors"
/>
<TextView
android:id="@+id/phrases"
style="@style/CategoryStyle"
android:background="@color/category_phrases"
android:text="@string/category_phrases"
/>
</LinearLayout>
here is the crash log:
03-17 23:48:09.416 902-902/com.example.android.miwok_startercode301 E/Zygote: v2 03-17 23:48:09.416 902-902/com.example.android.miwok_startercode301 I/libpersona: KNOX_SDCARD checking this for 10221 03-17 23:48:09.416 902-902/com.example.android.miwok_startercode301 I/libpersona: KNOX_SDCARD not a persona 03-17 23:48:09.417 902-902/com.example.android.miwok_startercode301 E/Zygote: accessInfo : 0 03-17 23:48:09.417 902-902/com.example.android.miwok_startercode301 W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2], Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.0_0009, [-1 -1 -1 -1 0 1] 03-17 23:48:09.419 902-902/com.example.android.miwok_startercode301 I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.example.android.miwok_startercode301 03-17 23:48:09.421 902-902/com.example.android.miwok_startercode301 I/art: Late-enabling -Xcheck:jni 03-17 23:48:09.703 902-902/com.example.android.miwok_startercode301 I/InstantRun: starting instant run server: is main process 03-17 23:48:09.918 902-902/com.example.android.miwok_startercode301 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 03-17 23:48:10.089 902-902/com.example.android.miwok_startercode301 D/TextView: setTypeface with style : 0 03-17 23:48:10.090 902-902/com.example.android.miwok_startercode301 D/TextView: setTypeface with style : 0 03-17 23:48:10.132 902-902/com.example.android.miwok_startercode301 D/Choreographer: init sf_choreo_doframe debug_Level : 0x4f4cdebug_game_running : false 03-17 23:48:10.143 902-902/com.example.android.miwok_startercode301 D/ViewRootImpl@4bc988c[MainActivity]: ThreadedRenderer.create() translucent=false 03-17 23:48:10.148 902-902/com.example.android.miwok_startercode301 D/InputTransport: Input channel constructed: fd=72 03-17 23:48:10.148 902-902/com.example.android.miwok_startercode301 D/ViewRootImpl@4bc988c[MainActivity]: setView = DecorView@30fcbd5[MainActivity] touchMode=true 03-17 23:48:10.151 902-902/com.example.android.miwok_startercode301 D/ViewRootImpl@4bc988c[MainActivity]: dispatchAttachedToWindow 03-17 23:48:10.197 902-902/com.example.android.miwok_startercode301 D/ViewRootImpl@4bc988c[MainActivity]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,0][1080,1920] result=0x27 surface={isValid=true 519920009728} surfaceGenerationChanged=true 03-17 23:48:10.197 902-902/com.example.android.miwok_startercode301 D/ViewRootImpl@4bc988c[MainActivity]: mHardwareRenderer.initialize() mSurface={isValid=true 519920009728} hwInitialized=true 03-17 23:48:10.204 902-985/com.example.android.miwok_startercode301 I/OpenGLRenderer: Initialized EGL, version 1.4 03-17 23:48:10.204 902-985/com.example.android.miwok_startercode301 D/OpenGLRenderer: Swap behavior 1 03-17 23:48:10.210 902-985/com.example.android.miwok_startercode301 D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [1080x1920]-format:1 03-17 23:48:10.241 902-902/com.example.android.miwok_startercode301 W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 03-17 23:48:10.249 902-902/com.example.android.miwok_startercode301 D/ViewRootImpl@4bc988c[MainActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1080, 1920) ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1 03-17 23:48:10.249 902-902/com.example.android.miwok_startercode301 D/ViewRootImpl@4bc988c[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 03-17 23:48:10.249 902-902/com.example.android.miwok_startercode301 D/ViewRootImpl@4bc988c[MainActivity]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 519920009728} 03-17 23:48:10.250 902-902/com.example.android.miwok_startercode301 V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@992dfea nm : com.example.android.miwok_startercode301 ic=null 03-17 23:48:10.250 902-902/com.example.android.miwok_startercode301 I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus 03-17 23:48:10.254 902-916/com.example.android.miwok_startercode301 D/InputTransport: Input channel constructed: fd=80 03-17 23:48:10.296 902-902/com.example.android.miwok_startercode301 V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@cbed2db nm : com.example.android.miwok_startercode301 ic=null 03-17 23:48:18.592 902-902/? D/ViewRootImpl@4bc988c[MainActivity]: ViewPostImeInputStage processPointer 0 03-17 23:48:18.596 902-902/? E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]] 03-17 23:48:18.596 902-902/? V/BoostFramework: BoostFramework() : mPerf = null 03-17 23:48:18.637 902-902/? D/ViewRootImpl@4bc988c[MainActivity]: ViewPostImeInputStage processPointer 1 03-17 23:48:18.687 902-902/? D/ViewRootImpl@4bc988c[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 03-17 23:48:18.738 902-902/? D/TextView: setTypeface with style : 0 03-17 23:48:18.739 902-902/? D/TextView: setTypeface with style : 0 03-17 23:48:18.799 902-902/? D/AndroidRuntime: Shutting down VM 03-17 23:48:18.800 902-902/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.miwok_startercode301, PID: 902 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.miwok_startercode301/com.example.android.miwok_startercode301.NumbersActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference at com.example.android.miwok_startercode301.NumbersActivity.onCreate(NumbersActivity.java:34) at android.app.Activity.performCreate(Activity.java:6956) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)