-3

I have below some code which will add the text 'test' to the textbox when the page loads, but for some reason it is throwing a null pointer; as if to say it can't find the control, or it is pointing to the incorrect file?

Java:

    package com.example.application;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
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.widget.Toast;
import android.os.Build;

public class ShowTeamsChosen extends ActionBarActivity {

    //This is the textbox I am populating...
    TextView txtOne = (TextView) findViewById(R.id.textView1);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_teams_chosen);
        //here I am setting the text
        txtOne.setText("test");
        readFromFile();

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

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.application.ShowTeamsChosen"
    tools:ignore="MergeRootFrame" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</FrameLayout>
anaximander
  • 7,083
  • 3
  • 44
  • 62
user3744373
  • 25
  • 1
  • 8

4 Answers4

4
  //This is the textbox I am populating...
    TextView txtOne = (TextView) findViewById(R.id.textView1);

move the initialization inside onCreate of txtOne, after setContentView

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
2

Change your code to this:

    public class ShowTeamsChosen extends ActionBarActivity {

    //This is the textbox I am populating...


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_teams_chosen);
        TextView txtOne = (TextView) findViewById(R.id.textView1);
        //here I am setting the text
        txtOne.setText("test");
        readFromFile();

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }
Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
1

Move:

TextView txtOne = (TextView) findViewById(R.id.textView1);

inside onCreate()

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
0

write TextView txtOne = (TextView) findViewById(R.id.textView1); after setContentView(R.layout.activity_show_teams_chosen);

Jogendra Gouda
  • 405
  • 4
  • 17