Search code examples
androidandroid-fragmentstextview

android setText crashes my app


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.


Solution

  • 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);