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 trial
Fahman Khan
3,798 PointsAndroid Finding View Components (R.id...)
I know the error is in this piece of following code.
TextView my=(TextView)findViewById(R.id.textId);
I have searched everywhere on google to resolve this issue, but none of those remedies seems to fixing my problem at all. I keep getting a nullpointer exception which I don't know why. 5 weeks ago, I had no such problem like this; however, I first encountered this problem yesterday. I have even reinstalled android studio but the problem still won't go away.
Here is my mainactivty. java code.
package com.example.fahman_khan75.testing;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.app.Activity;
public class MainActivity extends Activity { TextView my=(TextView)findViewById(R.id.textId); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
}
Here is my android manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.fahman_khan75.testing" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Finally, here is my XML file
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textId" />
</RelativeLayout>
1 Answer
Jon Kussmann
Courses Plus Student 7,254 PointsHi Fahman,
Your findViewById needs to be inside the onCreate method like so:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView my=(TextView)findViewById(R.id.textId);
}
I hope this helps. If not let me know.