Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGrigorij Schleifer
10,365 PointsFATAL EXCEPTION
Dear community,
My plan is to type a text into a TextView and display this text in a different activity´s TextView.
I get this nasty error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.nexuskiller.interactivestory/com.example.nexuskiller.interactivestory.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
This is my code:
MainActivity
package com.example.nexuskiller.interactivestory;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView mTextView = (TextView) findViewById(R.id.textView);
Button mButton = (Button) findViewById(R.id.button);
private String mText = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mText = mTextView.getText().toString();
callNewActivity(mText);
}
});
}
public void callNewActivity(String text) {
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra(getString(R.string.name), text);
startActivity(intent);
}
}
secondActivity
package com.example.nexuskiller.interactivestory;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class secondActivity extends Activity {
public static final String TAG = secondActivity.class.getSimpleName();
TextView secondTW = (TextView) findViewById(R.id.secondEditText);
String stringFromTW = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
stringFromTW = intent.getStringExtra(getString(R.string.name));
secondTW.setText(stringFromTW);
}
}
Any help is highly appreciated :)
Grigorij
1 Answer
Kourosh Raeen
23,733 PointsHi Grigorij - The calls to findViewById need to be inside onCreate() and after the call to setContentView(), so your first activity should look like this:
package com.example.nexuskiller.interactivestory;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView mTextView;
private Button mButton;
private String mText = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mText = mTextView.getText().toString();
callNewActivity(mText);
}
});
}
public void callNewActivity(String text) {
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra(getString(R.string.name), text);
startActivity(intent);
}
}
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHi Kourosh,
thank you sooo much. You saved my head from turning into a giant black hole
Grigorij