Reputation: 904
I'm very new to Android. I did read many Tutorials, and also the Google example about building first app.
I was able to correctly run the app only once! The problem is as written in the title of this question. I checked every question and answer provided here on the forum, but I can't figure what's going on.
The problem is simple. I started a new project with Eclipse and added a id to the TextView
. When I try to get the view I receive a null pointer.
This is the 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="com.cguida1978it.prova.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
And this is the MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main );
TextView t = (TextView)findViewById( R.id.text_view );
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
I checked also in the R.java
and the text_view
is in the id class
I try to post the error log:
05-03 16:04:22.729: E/AndroidRuntime(2123): FATAL EXCEPTION: main
05-03 16:04:22.729: E/AndroidRuntime(2123): Process: com.cguida1978it.prova, PID:2123
05-03 16:04:22.729: E/AndroidRuntime(2123): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cguida1978it.prova/com.cguida1978it.prova.MainActivity}: java.lang.NullPointerException
05-03 16:04:22.729: E/AndroidRuntime(2123): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-03 16:04:22.729: E/AndroidRuntime(2123): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 16:04:22.729: E/AndroidRuntime(2123): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 16:04:22.729: E/AndroidRuntime(2123): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 16:04:22.729: E/AndroidRuntime(2123): at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 16:04:22.729: E/AndroidRuntime(2123): at android.os.Looper.loop(Looper.java:136)
05-03 16:04:22.729: E/AndroidRuntime(2123): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 16:04:22.729: E/AndroidRuntime(2123): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 16:04:22.729: E/AndroidRuntime(2123): at java.lang.reflect.Method.invoke(Method.java:515)
05-03 16:04:22.729: E/AndroidRuntime(2123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 16:04:22.729: E/AndroidRuntime(2123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 16:04:22.729: E/AndroidRuntime(2123): at dalvik.system.NativeStart.main(Native Method)
05-03 16:04:22.729: E/AndroidRuntime(2123): Caused by: java.lang.NullPointerException
05-03 16:04:22.729: E/AndroidRuntime(2123): at com.cguida1978it.prova.MainActivity.onCreate(MainActivity.java:23)
05-03 16:04:22.729: E/AndroidRuntime(2123): at android.app.Activity.performCreate(Activity.java:5231)
05-03 16:04:22.729: E/AndroidRuntime(2123): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-03 16:04:22.729: E/AndroidRuntime(2123): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-03 16:04:22.729: E/AndroidRuntime(2123): ... 11 more
05-03 16:04:24.989: I/Process(2123): Sending signal. PID: 2123 SIG: 9
Can anyone of you explain me what could be the problem?
Is it an Eclipse issue/bug? May I reset Eclipse to default settings? When I started studying Android it worked a few times... why?? I'm a little bit confused...
Thank you very much!
EDIT TheFRedFox here are my imports
import android.app.Activity;
import android.widget.*;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.view.View.OnClickListener;
EDIT 2
This is a piace of the R.java
public static final class id {
public static final int action_settings=0x7f080002;
public static final int container=0x7f080000;
public static final int text_view=0x7f080001;
}
public static final class layout {
public static final int activity_main=0x7f030000;
public static final int fragment_main=0x7f030001;
}
And this is the AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cguida1978it.prova"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.cguida1978it.prova.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>
I also tried to retrieve the fragment in the onCreate()
method using the getFragmentManager().findFragmentById()
but also the fragment results in a null pointer!
I also tried to delete the content of R.java
as written in one of your answer here in the forum to force Eclipse to regenerate it, but the problem persists!
I really can't figure what is going wrong here. This is a near blank-porject, created from scratch! The only edits are the addition of the TextView
id and the lines to retrieve it in the Java code!
Oh... Maybe I did not mention (or it is not clear), I started many new projects from scratch and not modified, but the id and relative codes. Each new project has this issue!
Do you think is an Eclipse issue or ADT/AVD (virtual device) issue? Is there any other IDE I could use?
Really I'm a visually-impaired developer so, even in Eclipse I do not use the graphics designer. I always edit XML files, so I was wondering if it is easy to compile from command line and debug. I read about that in the "Getting started" Google guida. Before starting to read more deep I ask you if you would suggest it!
EDIT 3
The laalto answer seems to fix the problem. Inside the MainActivity
class I found a PlaceHolder
class that extends Fragment
class and moving the TextView
stuff inside the onCreateView()
using rootView.findViewById()
I can set the text property of the TextView
.
Why Eclipse does not create a separate class for the fragment fragment_main
? Instead it creates a generic class inside the MainActivity.java
?
Do you think I should create a separate class for fragments? or use the activity class as suggested in the answer 2?
In the Google's sample they extends the MainActivity
class using the activity bar that is a subclass of Fragment
, but, really, I was not able to retrieve resources inside the simple onCreate()
of the MainActivity
class...
This is causing me a little confusion...
Thank you for all of your answer and for your further explanation... I really appreciate your effort...
Upvotes: 1
Views: 1669
Reputation: 152817
Your text_view
is in the fragment layout and not in the activity layout. It won't be in the activity view hierarchy yet in onCreate()
and findViewById()
won't find it, returning null
. The NPE in your stacktrace is caused by trying to invoke a method on this null
.
Move the findViewById()
and related code to the fragment's onCreateView()
, calling findViewById()
on the rootView
inflated there.
Upvotes: 1
Reputation: 710
I can't comment yet, so I have to post an answer. ^^
Can you show us your imports. Sometimes Eclipse adds accidentally not your R class but the android's one. In this android.R class your id surely is not referenced. So you'll have to import your R class (when this is the problem).
EDIT:
Thanks for your imports. I have to correct myself. Your R class doesn't need to be imported as it is automatically chosen. One silly question: It is the right layout you are setting as the content view, right? When it is the right layout, I unfortunately don't know more yet. But I would give you two links to read to regenerate the R.class and/or fix some problem using adt version 22, which cause into some strange errors:
Regenerate R.class: https://stackoverflow.com/a/12195532/2625488
Problems with adt version 22: Eclipse error: R cannot be resolved to a variable
Upvotes: 0