0

Tried to make a combination of textview elements to show/hide spoilers in text. Problem is that application crashes after start if I add code to initially hide view with text. Tried both setHeight and LayoutProperties, both cause application to crash or somehow override OnClickListener event and block attempts to resize height of TextView.

Here's the code.

 String sample;
 boolean SpoilerOpen;
 int TextHeight;
 TextView Sample;
 String spoilertext;
 TextView SpoilerContainer;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  LinearLayout scroll = new LinearLayout (this);
  scroll.setOrientation(1);
  spoilertext="hidden txt";

  final String Spoiler_close="show";
  final String Spoiler_open="hide";
  SpoilerContainer=new TextView (this);
  SpoilerContainer.setText(Spoiler_close);
  SpoilerContainer.setClickable(true);
  SpoilerOpen=false;
  Sample=new TextView(this);
  Sample.setText(spoilertext);
// --part causing problems--
      ViewGroup.LayoutParams params = Sample.getLayoutParams();
      params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
      Sample.setLayoutParams(params);
// --end of part--
  SpoilerContainer.setOnClickListener(new View.OnClickListener() {
  public void onClick (View V)
  {
   if (SpoilerOpen==false)
  {
    SpoilerOpen=true;
    SpoilerContainer.setText(Spoiler_open);
    ViewGroup.LayoutParams params = Sample.getLayoutParams();
    params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    Sample.setLayoutParams(params);

  }
   else
  {
    SpoilerOpen=false;
    SpoilerContainer.setText(Spoiler_close);
    ViewGroup.LayoutParams params = Sample.getLayoutParams();
    params.height = 0;
    Sample.setLayoutParams(params); 

  }

  }
  });
  scroll.addView(SpoilerContainer);
  scroll.addView(Sample);
  setContentView(scroll);

 }

Found one similar questions solved here but in my case parent of TextView is LinearLayout and using its LayoutParams didn't help or I used it incorrectly. Hope I've provided enough information to find solution.

Here are logs from Eclipse.

09-09 15:44:27.288: D/AndroidRuntime(568): Shutting down VM
09-09 15:44:27.307: W/dalvikvm(568): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
09-09 15:44:27.347: E/AndroidRuntime(568): FATAL EXCEPTION: main
09-09 15:44:27.347: E/AndroidRuntime(568): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project/com.project.MainActivity}: java.lang.NullPointerException
09-09 15:44:27.347: E/AndroidRuntime(568):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
09-09 15:44:27.347: E/AndroidRuntime(568):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-09 15:44:27.347: E/AndroidRuntime(568):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-09 15:44:27.347: E/AndroidRuntime(568):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-09 15:44:27.347: E/AndroidRuntime(568):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 15:44:27.347: E/AndroidRuntime(568):  at android.os.Looper.loop(Looper.java:137)
09-09 15:44:27.347: E/AndroidRuntime(568):  at android.app.ActivityThread.main(ActivityThread.java:4340)
09-09 15:44:27.347: E/AndroidRuntime(568):  at java.lang.reflect.Method.invokeNative(Native Method)
09-09 15:44:27.347: E/AndroidRuntime(568):  at java.lang.reflect.Method.invoke(Method.java:511)
09-09 15:44:27.347: E/AndroidRuntime(568):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-09 15:44:27.347: E/AndroidRuntime(568):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-09 15:44:27.347: E/AndroidRuntime(568):  at dalvik.system.NativeStart.main(Native Method)
09-09 15:44:27.347: E/AndroidRuntime(568): Caused by: java.lang.NullPointerException
09-09 15:44:27.347: E/AndroidRuntime(568):  at com.project.MainActivity.onCreate(MainActivity.java:42)
09-09 15:44:27.347: E/AndroidRuntime(568):  at android.app.Activity.performCreate(Activity.java:4465)
09-09 15:44:27.347: E/AndroidRuntime(568):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-09 15:44:27.347: E/AndroidRuntime(568):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-09 15:44:27.347: E/AndroidRuntime(568):  ... 11 more
09-09 15:46:02.108: I/Process(568): Sending signal. PID: 568 SIG: 9
Community
  • 1
  • 1

1 Answers1

1

The NullPointerException error was caused because of the following line:

ViewGroup.LayoutParams params = Sample.getLayoutParams();

Since Sample didn't have any LayoutParams this causes the NullPointerException. Change this line into following:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.La‌​youtParams.MATCH_PARENT);
Metehan
  • 729
  • 5
  • 22
  • This doesn't solve the problem, apk still crashes. I assume that initially `scroll` is associated with new freshly created layout. In your variant it is linked to the existing one. But for application this changes nothing, as it still crashes with almost same error (this time it says `com.project.MainActivity.onCreate(MainActivity.java:42)` earlier it was `java:69`). – Andrey Dragunov Sep 10 '14 at 09:46
  • 1
    Thanks for advice, followed it, got two more crashes, but figured out working code. Idea is that `ViewGroup.LayoutParams` should be also changed to `LinearLayout.LayoutParams` and string with announcement of new params should look like this: `LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);` – Andrey Dragunov Sep 11 '14 at 08:15