2

I've got problem with using setText() method on Android 16 (4.1) app. I'm trying to put programatically anything into TextView tvopis (here's just some test string, later I plan on putting there string from intent). When I open activity opis, the app crashes. If I comment setText() part, everything works fine.

Here's java code

package com.example.pierwszyandroid;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;

public class OpisActivity extends Activity {

    public TextView tvopis;
    String txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opis);
         tvopis = (TextView) findViewById(R.id.tvotest);

         tvopis.setText("txt test");


        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

Here's fragment_opis.xml for this activity:

<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.example.pierwszyandroid.OpisActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/tvotest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

and this is activity_opis.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.pierwszyandroid.OpisActivity"
    tools:ignore="MergeRootFrame" />

and here's logcat with error message:

04-18 10:44:59.665: I/Choreographer(723): Skipped 206 frames!  The application may be doing too much work on its main thread.
04-18 10:46:34.016: I/Choreographer(723): Skipped 41 frames!  The application may be doing too much work on its main thread.
04-18 10:46:34.135: D/AndroidRuntime(723): Shutting down VM
04-18 10:46:34.135: W/dalvikvm(723): threadid=1: thread exiting with uncaught exception (group=0x2bc9a300)
04-18 10:46:34.155: E/AndroidRuntime(723): FATAL EXCEPTION: main
04-18 10:46:34.155: E/AndroidRuntime(723): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pierwszyandroid/com.example.pierwszyandroid.OpisActivity}: java.lang.NullPointerException
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.os.Looper.loop(Looper.java:137)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-18 10:46:34.155: E/AndroidRuntime(723):  at java.lang.reflect.Method.invokeNative(Native Method)
04-18 10:46:34.155: E/AndroidRuntime(723):  at java.lang.reflect.Method.invoke(Method.java:511)
04-18 10:46:34.155: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-18 10:46:34.155: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-18 10:46:34.155: E/AndroidRuntime(723):  at dalvik.system.NativeStart.main(Native Method)
04-18 10:46:34.155: E/AndroidRuntime(723): Caused by: java.lang.NullPointerException
04-18 10:46:34.155: E/AndroidRuntime(723):  at com.example.pierwszyandroid.OpisActivity.onCreate(OpisActivity.java:25)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.Activity.performCreate(Activity.java:5008)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-18 10:46:34.155: E/AndroidRuntime(723):  ... 11 more

Any help would be really appreciated, because I couldn't find any answer on already posted similar problems.

upenpat
  • 685
  • 3
  • 12
N.M.W
  • 33
  • 1
  • 5
  • Logcat: `onCreate(OpisActivity.java:25)` = line 25 in onCreate something returns null. It's obviously `tvopis`. – Blo Apr 18 '14 at 10:54

6 Answers6

2

If you look at the log cat, you will see that you have a null pointer exception when you try to set the text to the text view (line 25 in your OpisActivity).

This means that the txt view is not found in the layout and thus cannot be initialised. Since your textview is in the fragment, you have to first add the fragment to the layout and then find the view.

To solve your problem, move the findById and setText method after the fragment initialisation in your onCreate method, like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opis);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }

     tvopis = (TextView) findViewById(R.id.tvotest);
     tvopis.setText("txt test");
}
peshkira
  • 6,069
  • 1
  • 33
  • 46
  • It didn't help, application still crashes as before – N.M.W Apr 18 '14 at 11:37
  • then your PlaceholderFragment is not setup correctly. Do you set the contentView in the PlaceholderFragment to fragment_opis.xml? – peshkira Apr 18 '14 at 12:25
  • I've changed setContentView to (R.layout.fragment_opis) and deleted if (sevedInstancestate==null){...}, and now it's working fine. – N.M.W Apr 18 '14 at 13:55
1

Your app crashed because your TextView tvotest belong to fragment_opis.xml layout and you have to set activity_opis.xml file to your activity.

so initialized PlaceholderFragment() first and then initialized your TextView tvotest or change your layout like

setContentView(R.layout.fragment_opis.xm)
M D
  • 47,665
  • 9
  • 93
  • 114
0

You have wrongly set your layout in your onCreate() method. You TextView resides inside layout fragment_opis.xml whereas you have set layout as setContentView(R.layout.activity_opis);.

Just change your setContentView(R.layout.activity_opis); line

to as below

  setContentView(R.layout. fragment_opis);
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

change this setContentView(R.layout.activity_opis);

to this

setContentView(R.layout.fragment_opis.xml);

Vamshi
  • 1,495
  • 1
  • 15
  • 31
0

Sometimes when creating layouts in fragments you copy and paste xml code that has duplicate IDs. When you then call the id of the pasted resources it will try to invoke the first defined resources which is probably null at runtime. Double check if the TextView's id is found only in one fragment's Xml. If not work around "findviewbyid".I hope that helps

NgoniDee
  • 1
  • 2
0

try checking out the answer at https://stackoverflow.com/a/20177019/14229871 .I was encountering a similar problem with textView, every time I call set text my app would crash. I think it's a matter of how u call set text.

Amos Banda
  • 11
  • 1