0

I've got a problem which makes me crazy because it looks like a simple one ... When I try to getting a view by it's ressources (a simple operation that i've done multiples times before ) with findRessourcesById, it throwns a NullPointerException when I access in onCreate of my main activity, or a RessourcesNotFoundException when I do this in my PagerAdapter. In facts it doesn't work anywhere ... Here my mainActivity :

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;

public class MainActivity extends Activity {
ViewPager myPager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Fiche f = new Fiche();
    FicheAdapter adapter;
    adapter = new FicheAdapter(f);
    myPager = (ViewPager) findViewById(R.id.pager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);

    TextView tv = (TextView) findViewById(R.id.nom);
    // HERE IT THROWNS EXCEPTION
    tv.setText("Hello ?");
}

my R.java file :

public final class R {
        .. ...
public static final class id {
    ... ... ...
    public static final int matricule=0x7f080019;
    public static final int menu_settings=0x7f08001d;
    public static final int nom=0x7f08001a;
    public static final int pager=0x7f080001;
    public static final int prenom=0x7f08001b;
    ... ... 
    }
}

I dont think I need to show you my layout , because Eclipse would have seen it if there was a wrong id ... ;) (it's ok) I've tried "clean", but no effects ... I've had a similar error before with an import of R of another project but here no imports of R are made...

I'd appreciate any help, because i'm stuck here . !!

Thanks a lot

Nico

EDIT : my log => where there is CausedBy NUll Pointer Exception at mainactivity : 27 , this is the line i've marked on my code above ... 07-15 19:54:56.460: E/AndroidRuntime(22836): FATAL EXCEPTION: main 07-15 19:54:56.460: E/AndroidRuntime(22836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.main.courante.e/com.example.main.courante.e.MainActivity}: java.lang.NullPointerException 07-15 19:54:56.460: E/AndroidRuntime(22836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751) 07-15 19:54:56.460: E/AndroidRuntime(22836): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767) 07-15 19:54:56.460: E/AndroidRuntime(22836): at android.app.ActivityThread.access$1500(ActivityThread.java:122) 07-15 19:54:56.460: E/AndroidRuntime(22836): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005) 07-15 19:54:56.460: E/AndroidRuntime(22836): at android.os.Handler.dispatchMessage(Handler.java:99) 07-15 19:54:56.460: E/AndroidRuntime(22836): at android.os.Looper.loop(Looper.java:132) 07-15 19:54:56.460: E/AndroidRuntime(22836): at android.app.ActivityThread.main(ActivityThread.java:4028) 07-15 19:54:56.460: E/AndroidRuntime(22836): at java.lang.reflect.Method.invokeNative(Native Method) 07-15 19:54:56.460: E/AndroidRuntime(22836): at java.lang.reflect.Method.invoke(Method.java:491) 07-15 19:54:56.460: E/AndroidRuntime(22836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 07-15 19:54:56.460: E/AndroidRuntime(22836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 07-15 19:54:56.460: E/AndroidRuntime(22836): at dalvik.system.NativeStart.main(Native Method) 07-15 19:54:56.460: E/AndroidRuntime(22836): Caused by: java.lang.NullPointerException 07-15 19:54:56.460: E/AndroidRuntime(22836): at com.example.main.courante.e.MainActivity.onCreate(MainActivity.java:27) 07-15 19:54:56.460: E/AndroidRuntime(22836): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 07-15 19:54:56.460: E/AndroidRuntime(22836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715) and my main_layout.xml :

<LinearLayout 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:orientation="horizontal" >

 <fragment android:name="com.example.main.courante.e.DateList"
 android:id="@+id/list_frag"
 android:tag="list"
 android:layout_width="0dip"
 android:layout_height="fill_parent"
 android:layout_weight="1"/> 

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="3" />

</LinearLayout>

This is InstantiateItem method from my pageAdapter :

    public Object instantiateItem(View collection, int position) {

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;
    switch (position) {
    case 0:
        resId = R.layout.checks_matin;
        break;
    case 1:
        resId = R.layout.checks_soir;
        break;
    }

    View view = inflater.inflate(resId, null);
    TextView tv = (TextView) view.findViewById(R.id.matricule);    
    tv.setText(fiche.getMatricule());
    tv = (TextView) view.findViewById(R.id.nom);
    tv.setText(fiche.getNom());
    ((ViewPager) collection).addView(view, 0);

    return view;
}

Even here it thowns an exception whereas in both of my layout checks_matin/soir, the id nom/matricule etc exists ...

Nikkolasg
  • 444
  • 4
  • 18
  • 2
    Please post `activity_main.xml` and the logcat errors. (After you paste them into your question use Crtl+K to automatically format the code for SO.) – Sam Jul 15 '12 at 17:26
  • it looks like you have set some `background` for your layouts..If this is the case make sure you have those `images` in your `drawables` and open them..so that they are not corrupted.. – Mohsin Naeem Jul 15 '12 at 17:28
  • It's not that the problem. I've posted my layout & log – Nikkolasg Jul 15 '12 at 18:09

3 Answers3

0

the text view R.id.nom doesn't exist in the the xml you map with your activity using setContentView(R.layout.activity_main);

So when you try to refer it TextView tv = (TextView) findViewById(R.id.nom); , the compiler looks into the activity_main.xml file, hence the nullPointerException.

The id is mapped/exists in your R.java file because it is defined in another xml.

Vinay W
  • 9,912
  • 8
  • 41
  • 47
  • Yeah ok for mainActivity. I've added portion of my code for my pageAdapter where I try accessing same ressources, and it crashes too. see below EDIT portion – Nikkolasg Jul 15 '12 at 18:29
  • I mean , my pageadapter inflate the view where there are the textview with id nom matricule etc ... with RessourcesNotFoundException at the line : setText(...) – Nikkolasg Jul 15 '12 at 18:30
0

Do you have more than one xml layouts? If you do then it is likely that the findViewById is looking for this resource in wrong xml.

Noman Arain
  • 1,172
  • 4
  • 19
  • 45
0

OK I've found some useful links here : Android ViewPager findViewById not working - Always returning null

so here is the starting of my checks_matin.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/linear" >



<!--  <include xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/top_layout"
    android:id="@+id/inc_top" /> -->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:id="@+id/top_lin" >



    <TextView
        android:id="@+id/matricule"
        style="@style/textview_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="matricule"
        android:layout_marginLeft="25dip" 
        android:layout_marginRight="25dip" />

i've commented the include to test... and here my new java code in instantiateItem :

 LinearLayout view = (LinearLayout)inflater.inflate(resId, null);
    LinearLayout v1 = (LinearLayout)view.findViewById(R.id.linear);
    LinearLayout v2 = (LinearLayout)v1.findViewById(R.id.top_lin);
    TextView tv = (TextView) v2.findViewById(R.id.matricule);    
    tv.setText(fiche.getMatricule());

But it STILL doesn't work !!! I'm out of ideas here ... :/

EDIT : IT does work !! juste remove this line from above :

LinearLayout v1 = (LinearLayout)view.findViewById(R.id.linear);
Community
  • 1
  • 1
Nikkolasg
  • 444
  • 4
  • 18