0

I have created a TextView block in the XML file of my MultipleID activity, it looks like the following (in relative layout):

    <TextView 
        android:id="@+id/multipleIDtext"
        android:text="@string/multipleIDtext_val"
        android:layout_width="wrap_context"
        android:layout_height="Wrap_context"
    />

In the MultipleID.java file, inside the onCreate() method, i have the following line:

    TextView textin = (TextView) findViewById(R.id.multipleIDtext);

And later on, still inside the onCreate() method, i have the following line:

  textin.setText(message+ikinci);

Where "message+ikinci" is a string value that is not null.

This returns a NullPointerException and i can't solve it.

Thanks in advance

If necessary,

Activity Code:

package com.example.yengeliogullariaileagaci;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MultipleID extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();

        Bundle b = intent.getExtras();
        String kendiIsmi= b.getString("isim");
        int isimkactane = b.getInt("sayi");
        int arrSayi = b.getInt("arrAdamSayisi");
        String message = kendiIsmi + " isminde aile agacinda " + String.valueOf(isimkactane) + " tane kisi bulunmaktadir \nLutfen hangisi oldugunuzu belirtiniz";
        StringBuilder s = new StringBuilder();
        TextView textin = (TextView) findViewById(R.id.multipleIDtext);


        for(int i=0;i<b.size()-3;i++)
        {
            s.append(b.getString("arrKendiIsmi"+i)+"   "+b.getString("arrAnne"+i)+"   "+b.getString("arrBaba"+i)+"\n");
        }

        String ikinci = "\n\n"+s.toString();
        //TextView text = new TextView(this);
        //text.setText(message+ikinci);
        //text.setTextSize(15);
        //setContentView(text);
        textin.setText(message+ikinci);

        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

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

}

Complete 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=".MultipleID" >


    <TextView 
        android:id="@+id/multipleIDtext"
        android:text="@string/multipleIDtext_val"
        android:layout_width="wrap_context"
        android:layout_height="Wrap_context"
    />


</RelativeLayout>

Log Cat Error Log:

09-08 14:15:12.546: E/AndroidRuntime(16292): FATAL EXCEPTION: main
09-08 14:15:12.546: E/AndroidRuntime(16292): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yengeliogullariaileagaci/com.example.yengeliogullariaileagaci.MultipleID}: java.lang.NullPointerException
09-08 14:15:12.546: E/AndroidRuntime(16292):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at android.app.ActivityThread.access$700(ActivityThread.java:152)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at android.os.Looper.loop(Looper.java:137)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at android.app.ActivityThread.main(ActivityThread.java:5328)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at java.lang.reflect.Method.invokeNative(Native Method)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at java.lang.reflect.Method.invoke(Method.java:511)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at dalvik.system.NativeStart.main(Native Method)
09-08 14:15:12.546: E/AndroidRuntime(16292): Caused by: java.lang.NullPointerException
09-08 14:15:12.546: E/AndroidRuntime(16292):    at com.example.yengeliogullariaileagaci.MultipleID.onCreate(MultipleID.java:41)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at android.app.Activity.performCreate(Activity.java:5250)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
09-08 14:15:12.546: E/AndroidRuntime(16292):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-08 14:15:12.546: E/AndroidRuntime(16292):    ... 11 more
7
  • I'm surprised that this even compiles. Your TextView definition is missing height and width which must be specified. Try cleaning the project and building again. I suspect that it will not build. You should also take some time out to learn some Java basics. textin can only ever be a TextView since that is how you declare it. null simply means that it has not been initialised which in turn means that this - (TextView) findViewById(R.id.multipleIDtext); - returns null. Commented Sep 8, 2013 at 11:21
  • Thanks for the answer, you're right about the layout but i have the layout params set in the xml, i didn't put them in here to keep it simple and just post the relevant stuff (i hate to see 1000 lines of code posted here in the questions myself). The layout parameters doesn't have anything to do with the nullpointerexception though, right? How can i do the initialization, with textin = new TextView(); ?? Commented Sep 8, 2013 at 11:27
  • @BurakSoner post your activity code Commented Sep 8, 2013 at 11:32
  • Please show the entire onCreate(), post the stack trace from logcat and the complete layout XML. Commented Sep 8, 2013 at 11:35
  • @Raghunandan i have now Commented Sep 8, 2013 at 11:35

1 Answer 1

1

You are missing setContentView

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentview(R.layout.mylayout);

You need to set the content of your layout to the activity first and then initialize views. findViewById() looks for a View with the supplied ID in the currently inflated layout.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.