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 ...