Hello everybody! Wrote the program. It seems everything is correct, but I can not understand why the error. Showed the code to the programmer, he also did not find anything like that. MainActivity code:

import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.PopupWindow; import android.widget.Spinner; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Random; public class MainActivity extends AppCompatActivity { ArrayList<String> spinner1 = new ArrayList<>(); ArrayList<String> spinner2 = new ArrayList<>(); EditText editText; ArrayAdapter<String> adapter1; ArrayAdapter<String> adapter2; Spinner mainSpinner; Spinner newSpinner; Button button; int i = 1; String CurrentText; int width = 0; int ran1; int ran2; int ran3; int ran4; int ran5; int ran6; int ran7; int ran8; String str1; String str2; String str3; String str4; String str5; String str6; String str7; String str8; String str9; String str10; boolean block = true; Random r = new Random(); TextView textview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.EditText); mainSpinner = (Spinner) findViewById(R.id.MainSpinner); newSpinner = (Spinner) findViewById(R.id.NewSpinner); button = (Button) findViewById(R.id.Button); final DisplayMetrics metrics = getResources().getDisplayMetrics(); width = metrics.widthPixels; textview = (TextView) findViewById(R.id.textView); // адаптер MainSpinner adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner1); adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mainSpinner.setAdapter(adapter1); // адаптер NewSpinner adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner2); adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); newSpinner.setAdapter(adapter2); } private void initiatePopupWindow() { try { textview = (TextView) findViewById(R.id.textView); LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.standardscrapyard, (ViewGroup) findViewById(R.id.popup_element)); PopupWindow pwindo = new PopupWindow(layout, width, 180, true); pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0); } catch (Exception e) { e.printStackTrace(); } } public void delay() { try { Thread.sleep(600); } catch (InterruptedException e) { e.printStackTrace(); } } public void onClick(View button) { CurrentText = (editText.getText().toString()); if (i == 1) { spinner1.add(editText.getText().toString()); adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mainSpinner.setAdapter(adapter1); } else { spinner2.add(editText.getText().toString()); adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); newSpinner.setAdapter(adapter2); } if (CurrentText.equals("12345")) { newSpinner.setVisibility(View.VISIBLE); i = 2; } if (CurrentText.equals("2014")) { ran8 = r.nextInt(90000000) + 10000000; ran7 = r.nextInt(9000000) + 1000000; ran6 = r.nextInt(900000) + 100000; ran5 = r.nextInt(90000) + 10000; ran4 = r.nextInt(9000) + 1000; ran3 = r.nextInt(900) + 100; ran2 = r.nextInt(90) + 10; ran1 = r.nextInt(9) + 1; str1 = String.valueOf(ran8); str2 = "0" + (String.valueOf(ran7)); str3 = "0" + "0" + (String.valueOf(ran6)); str4 = "0" + "0" + "0" + (String.valueOf(ran5)); str5 = "0" + "0" + "0" + "0" + (String.valueOf(ran4)); str6 = "0" + "0" + "0" + "0" + "0" + (String.valueOf(ran3)); str7 = "0" + "0" + "0" + "0" + "0" + "0" + (String.valueOf(ran2)); str8 = "0" + "0" + "0" + "0" + "0" + "0" + "0" + (String.valueOf(ran1)); str9 = "0" + "0" + "0" + "0" + "0" + "0" + "0" + "1"; str10 = Integer.toString(R.string.scrval); Toast.makeText(getBaseContext(), "Режим помойки включен", Toast.LENGTH_SHORT).show(); textview.setText(str1); initiatePopupWindow(); textview.setText(str2); delay(); textview.setText(str3); delay(); textview.setText(str4); delay(); textview.setText(str5); delay(); textview.setText(str6); delay(); textview.setText(str7); delay(); textview.setText(str8); delay(); textview.setText(str9); delay(); textview.setText(str10); } } } 

Do not pay attention that there is no setOnClickListener in the code. I put the attribute in xml android: onclick. The button works fine. Flies only if you enter "2014" and click on the button. All other values ​​work fine. The error occurs in these lines:

 textview.setText(str1); initiatePopupWindow(); textview.setText(str2); delay(); textview.setText(str3); delay(); textview.setText(str4); delay(); textview.setText(str5); delay(); textview.setText(str6); delay(); textview.setText(str7); delay(); textview.setText(str8); delay(); textview.setText(str9); delay(); textview.setText(str10); 

Checked the way of commenting lines. Even if you write this:

 textview.setText("Hello!"); 

Anyway, the same mistake. If you comment out these lines, everything works fine. Just in case, add the code strings.xml:

 <resources> <string name="app_name">Program 20000dgfg</string> <string name="LoadButtonText">Загрузить</string> <string name="CloseText">close</string> <string name="scrap">scrapyard</string> <string name="scrval">00000000</string> 

And, of course, standardscrapyard.xml:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/popup_element" android:background="#000000" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <ImageView android:layout_width="80dp" android:layout_height="180dp" android:id="@+id/imageView" android:src="@drawable/tbo063krishka" android:contentDescription="@string/scrap" android:layout_alignParentLeft="true" android:layout_alignParentStart="false" android:layout_marginLeft="12dp" android:layout_alignParentTop="true" /> <TextView android:layout_width="wrap_content" android:layout_height="66dp" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/scrval" android:id="@+id/textView" android:textColor="#00c10a" android:layout_marginTop="40dp" android:layout_marginLeft="140dp" android:width="120dp" android:layout_alignLeft="@+id/imageView" android:layout_alignParentRight="true" /> </RelativeLayout> 

Error code:

03-30 15: 42: 37.594 14262-14262 / com.testprogram.alex.program20000dgfg E / AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute for android: onClick at android.support.v7.app. AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java:275) at android.view.View.performClick (View.java:3511) at android.view.View $ PerformClick.run (View.java:14105) at android.os.Handler .handleCallback (Handler.java:605) at android.os.Handler.dispatchMessage (Handler.java:92) at android.os.Looper.loop (Looper.java:137) at android.app.ActivityThread.main (ActivityThread. java: 4600) at java.lang.reflect.Method.invokeNative (Native Method) at java.lang.reflect.Method.invoke (Method.java 12.11) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run ( ZygoteInit.java:809) at com.android.internal.os.ZygoteInit.main (ZygoteInit.javaCl76) at dalvik.system.NativeStart.main (Native Method) Caused by: java.lang.reflect.InvocationTargetException at java. lang.reflect.Me thod.invokeNative (Native Method) at java.lang.reflect.Method.invoke (Method.java sources11) at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java:270) at android.view. View.performClick (View.java:3511) at android.view.View $ PerformClick.run (View.java:14105) at android.os.Handler.handleCallback (Handler.java:605) at android.os.Handler.dispatchMessage (Handler.java:92) at android.os.Looper.loop (Looper.java:137) at android.app.ActivityThread.main (ActivityThread.java:4600) at java.lang.reflect.Method.invokeNative (Native Method ) at java.lang.reflect.Method.invoke (Method.java including11) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:809) at com.android.internal.os.ZygoteInit. main (ZygoteInit.java 5776) at dalvik.system.NativeStart.main (Native Method) Caused by: java.lang.NullPointerException at com.testprogram.alex.program20000dgfg.MainActivity.onClick (MainActivity.java:129) at java. lang.reflect.Method.invokeNative (Native Metho d) at java.lang.reflect.Method.invoke (Method.java sources11) at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java around 70) at android.view.View.performClick (View .java: 3511) at android.view.View $ PerformClick.run (View.java:14105) at android.os.Handler.handleCallback (Handler.java:605) at android.os.Handler.dispatchMessage (Handler.java: 92) at android.os.Looper.loop (Looper.java:137) at android.app.ActivityThread.main (ActivityThread.java:4600) at java.lang.reflect.Method.invokeNative (Native Method) at java.lang .reflect.Method.invoke (Method.java Dep11) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:809) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java : 576) at dalvik.system.NativeStart.main (Native Method)

Code activity_main.xml:

 <?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.testprogram.alex.program20000dgfg.MainActivity" android:clickable="true"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/LoadButtonText" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="81dp" android:enabled="true" android:onClick="onClick"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/EditText" android:layout_above="@+id/button" android:layout_marginBottom="38dp" android:layout_alignRight="@+id/button" android:layout_alignEnd="@+id/button" android:layout_alignLeft="@+id/button" android:layout_alignStart="@+id/button" android:inputType="text" /> <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/MainSpinner" android:layout_marginTop="60dp" android:layout_alignParentTop="true" android:layout_alignRight="@+id/EditText" android:layout_alignEnd="@+id/EditText" android:layout_alignLeft="@+id/EditText" android:layout_alignStart="@+id/EditText" android:popupBackground="#b60000" /> <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/NewSpinner" android:layout_marginBottom="35dp" android:layout_above="@+id/EditText" android:layout_alignLeft="@+id/EditText" android:layout_alignStart="@+id/EditText" android:layout_alignRight="@+id/EditText" android:layout_alignEnd="@+id/EditText" android:visibility="invisible" android:popupBackground="#a6ff9e" /> </RelativeLayout> 

Help me, please, deal with this error!

  • Thread.sleep (600); in the button click handler? Are you seriously? - KoVadim
  • What's wrong? Not in this line error, but in the lines: textview.setText (str). I want to make the label on the element change at intervals of 6/10 seconds. And I have a mistake. - HybridTC25
  • if you comment out all delay (), does it work? - KoVadim
  • Of course it works! Not in the delay () case, but in textview.setText (str). - HybridTC25
  • 2
    Well, I'm not a real android programmer, but Activity.findViewById returns a view from the layout that was loaded into setContentView , or null if there is none, which it does. You can be helped textview = (TextView) layout.findViewById(R.id.textView); in initiatePopupWindow , and transfer the initiatePopupWindow call before using the textview . Or maybe not, ask your programmer. - zRrr

1 answer 1

So from the error it is clear that your textview == null

Let's try to figure out why, for this we consider where it is initialized:

Place number 1 - onCreate ()

 textview = (TextView) findViewById(R.id.textView); 

Naturally here textview == null , because There is no TextView in activity_main.xml with id==textView .

Place number 2 - initiatePopupWindow ()

 textview = (TextView) findViewById(R.id.textView); 

Here we look again in activity_main.xml (!!!). And you need to look for TextView on the in-flute standardscrapyard.xml . Therefore, the initiatePopupWindow() code should look something like this:

 private void initiatePopupWindow() { try { LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.standardscrapyard, (ViewGroup) findViewById(R.id.popup_element)); textview = (TextView)layout.findViewById(R.id.textView); PopupWindow pwindo = new PopupWindow(layout, width, 180, true); pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0); } catch (Exception e) { e.printStackTrace(); } } 

But even after that, the code will still fall, since You access the textview before calling initiatePopupWindow() :

 textview.setText(str1); initiatePopupWindow(); 

Apparently it is necessary to swap these lines.

  • Thank you very much! The application works, but when I enter the same 2014 and press the button, the phone hangs and android immediately executes the last line: textview.setText(str10); . I want to make the label change every 600 milliseconds, and it immediately changes all the values ​​and only then, when the text has already reached the last value, a window appears with the last line. How to fix this incident? I tried to set the delay for 5 seconds - it does not help. :( Tell me , please! - HybridTC25
  • @ HybridTC25 you already wrote that using Thread.sleep(600); in the main thread is the reason for this behavior. - ermak0ff
  • How to fix it? Tell me please! I need to make the label on the layout change every time. - HybridTC25
  • @ HybridTC25 as an option, you can use Handler and its postDelayed() method - ermak0ff
  • How to add it to my code? I can not understand. I can’t do anything. - HybridTC25