The question is most likely very childish and stupid, but I have been puzzling for several days now, so please read it through. Already tried everything, perhaps the error lies in the very basics, which I very casually studied and immediately switched to coding.

I use YouTube API in my application, which requires YOUTUBE_API_KEY and a signed certificate. After signing, it became necessary to change the application a bit.

There is a MainActivity with two buttons that lead to Activity1 and Activity2. In Activity1, YOUTUBE_API_KEY, GridView adapter, etc. are used. Here everything works correctly.

Activity2 also works correctly, there is an adapter for marking GridView. But as soon as I change the layout (let's say I add the Button button) for Activity2, Activity1 stops working (WTF?!?), Namely NPE crashes. The layouts of these activites are different and not connected in any way, so I am a bit taken aback how this can be.

Here is the code where I initialize the adapter and which line the log refers to:

GridView gridview = (GridView) findViewById(R.id.gridViewVideo); gridview.setAdapter(new VideoAdapter(this)); 

Here is the error log:

09-25 09: 10: 13.322 15921-15921 / com422studio.vk.drawinglessons E / AndroidRuntime: FATAL EXCEPTION: main Process: com422studio.vk.drawinglessons, PID: 15921 java.lang.RuntimeException: Unable to start componentIntent26 {com422studio. vk.drawinglessons / com422studio.vk.drawinglessons.VideoChoose}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter (android.widget.ListAdapter)' on a null object reference android.app .ActivityThread.performLaunchActivity (ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2360) at android.app.ActivityThread.access $ 800 (ActivityThread.java:144) at android.app.ActivityThread $ H .handleMessage (ActivityThread.java:1278) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:135) at android.app.ActivityThread.main (ActivityThread. java: 5221) at java.lang.reflect.Method.invoke (Native Method) at java.lang.reflect.Method.invoke (Met hod.javaUE72) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694) Caused by: java .lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter (android.widget.ListAdapter)' on a null object reference at com422studio.vk.drawinglessons.VideoChoose.onCreate (VideoChoose.java:39) at android.app.Activity.performCreate (Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java Resources 251) at android.app.ActivityThread .handleLaunchActivity (ActivityThread.java:2360) at android.app.ActivityThread.access $ 800 (ActivityThread.java:144) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1278) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:135) at android.app.ActivityThread.main (ActivityThread.java:5221) at java.lang.reflect.Method.invoke (Native Method) at javag.reflect.Method.invoke (Method.java.7072) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 899) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694)

 public class VideoChoose extends AppCompatActivity { Toolbar toolbar; Button button; @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.video_choose_layout); initToolbar(); GridView gridview = (GridView) findViewById(R.id.gridViewVideo); gridview.setAdapter(new VideoAdapter(this)); gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(VideoChoose.this, VideoGallery.class); intent.putExtra("id", position); startActivity(intent); } }); } private void initToolbar(){ toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle("Видео уроки"); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener(){ @Override public boolean onMenuItemClick(MenuItem item) { return false; } }); toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back_icon)); toolbar.setNavigationOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(VideoChoose.this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }); } } 

video_choose_layout.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <GridView android:id="@+id/gridViewVideo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:gravity="center" android:horizontalSpacing="10dp" android:numColumns="2" android:stretchMode="columnWidth" android:verticalSpacing="10dp" > </GridView> </LinearLayout> 

UPDATE:

Activity2:

 public class ActivityGallery extends AppCompatActivity { Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gallery_layout); initToolbar(); GridView gridview = (GridView) findViewById(R.id.gridviewImage); gridview.setAdapter(new ImageAdapter(this, mThumbIds)); gridview.setOnItemClickListener(gridviewOnItemClickListener); } private GridView.OnItemClickListener gridviewOnItemClickListener = new GridView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(), FullActivityGallery.class); i.putExtra("id", position); startActivity(i); } }; private void initToolbar(){ toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle("Галерея"); toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back_icon)); toolbar.setNavigationOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(ActivityGallery.this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }); } } 

gallery_layout.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <GridView android:id="@+id/gridviewImage" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="100dp" android:numColumns="3" android:verticalSpacing="1dp" android:horizontalSpacing="1dp" android:stretchMode="columnWidth" android:gravity="center"/> </LinearLayout> 
  • most likely a matter of inattention, and you missed some trifle. VideoChoose have to lay out the VideoChoose code and the xml markup that VideoChoose uses - Vladyslav Matviienko
  • one
    Yes, and if you use Instant Run - disable it to hell with it. It is crooked, buggy and not working. - Vladyslav Matviienko
  • @metalurgus Yes, I understand that the problem is somewhere in the carelessness, but I can not find where I was wrong. I added the code. Tell me in which direction to dig - ivanovd422
  • one
    and the second layout and activity where? fill_parent? - Shwarz Andrei
  • @ShwarzAndrei Added, really too big a wall of code turned out .. - ivanovd422

0