简体   繁体   中英

Getting nullpointer with TextView

I am getting a nullPointerException when getting a textview in Android. It seems like somedays I can set TextViews and it will work and somedays it wont. Can someone explain to me as to why I would be getting a nullpointer from this? It is in the method setTextView() the first line, and it is being called from the Net class.

package com.tsunamistudios.computerwatch;

import java.io.IOException;
import java.io.OptionalDataException;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class Client extends Activity {

    private static String message;
    private static ArrayList<Program> programs = new ArrayList<Program>();
    private static Net net;
    private TextView txtView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);   
        new Thread(net = new Net()).start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.client, menu);
        return true;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        Client.message = message;
    }

    public ArrayList<Program> getPrograms() {
        return programs;
    }

    public void setTextView() {
        txtView = (TextView) findViewById(R.id.txtView);
        txtView.setText(getPrograms().get(0).getName() + getPrograms().get(0).getDescription());
    }

    public void setPrograms(ArrayList<Program> programs) {
        Client.programs = programs;
    }

    public Net getNet() {
        return net;
    }

    public void setNet(Net net) {
        Client.net = net;
    }


    public TextView getTxtView() {
        return txtView;
    }
}

Net.java

package com.tsunamistudios.computerwatch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OptionalDataException;
import java.io.PrintWriter;
import java.net.Socket;

public class Net extends Client implements Runnable {

    private static Socket echoSocket;
    private static PrintWriter out;
    private static BufferedReader in;
    private static ObjectInputStream inFromServer;

    @Override
    public void run() {
        try {
            String hostName = "192.168.0.105";
            int portNumber = 6984;
            echoSocket = new Socket(hostName, portNumber);
//          out = new PrintWriter(echoSocket.getOutputStream(), true);
//          in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
            inFromServer = new ObjectInputStream(echoSocket.getInputStream());   

            getPrograms().add((Program) getProgramsFromObject());           

            setTextView(getTxtView());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

    public static Socket getEchoSocket() {
        return echoSocket;
    }

    public Object getProgramsFromObject() {     
        try {
            if(getObjectInputStream() != null) {
                return getObjectInputStream().readObject();
            } else {
                return new Program("Nullpointer", "NullPointer", "Null", null);
            }
        } catch (OptionalDataException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    public static void setEchoSocket(Socket echoSocket) {
        Net.echoSocket = echoSocket;
    }

    public static PrintWriter getOut() {
        return out;
    }

    public static void setOut(PrintWriter out) {
        Net.out = out;
    }

    public static BufferedReader getIn() {
        return in;
    }

    public ObjectInputStream getObjectInputStream() {
        return inFromServer;
    }

    public static void setInFromServer(ObjectInputStream inFromServer) {
        Net.inFromServer = inFromServer;
    }

    public static void setIn(BufferedReader in) {
        Net.in = in;
    }

}

xml

<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=".Client" >

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="75dp"
        android:layout_marginTop="162dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Error

12-21 12:57:17.043: E/AndroidRuntime(30909): FATAL EXCEPTION: Thread-1266
12-21 12:57:17.043: E/AndroidRuntime(30909): Process: com.tsunamistudios.computerwatch, PID: 30909
12-21 12:57:17.043: E/AndroidRuntime(30909): java.lang.NullPointerException
12-21 12:57:17.043: E/AndroidRuntime(30909):    at android.app.Activity.findViewById(Activity.java:1884)
12-21 12:57:17.043: E/AndroidRuntime(30909):    at com.tsunamistudios.computerwatch.Client.setTextView(Client.java:47)
12-21 12:57:17.043: E/AndroidRuntime(30909):    at com.tsunamistudios.computerwatch.Net.run(Net.java:30)
12-21 12:57:17.043: E/AndroidRuntime(30909):    at java.lang.Thread.run(Thread.java:841)

Your Net class is declared as

public class Net extends Client implements Runnable {

By inheritance hierarchy, it also extends Activity which has a window instance variable which will be initialized to null by default. When you call

 setTextView(getTxtView());

it calls the inherited method

public void setTextView() {
    txtView = (TextView) findViewById(R.id.txtView);
    txtView.setText(getPrograms().get(0).getName() + getPrograms().get(0).getDescription());
}

But because your Net is an Activity that hasn't been given a Window by Android, it fails internally. The implementation of Activity is

public View findViewById(int id) {
    return getWindow().findViewById(id);
}

where getWindow() will return null .

You will have to rethink your design. Is Net really meant to be an Activity ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM