我遇到了一个让我抓狂的问题,因为它看起来很简单......当我尝试使用 findRessourcesById 通过它的资源(我之前做过多次的简单操作)获取视图时,它会抛出 NullPointerException当我访问我的主要活动的 onCreate 时,或者当我在我的 PagerAdapter 中执行此操作时访问 RessourcesNotFoundException。事实上,它在任何地方都不起作用......这是我的 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 ?");
}
我的 R.java 文件:
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;
... ...
}
}
我认为我不需要向您展示我的布局,因为如果 id 错误,Eclipse 会看到它......;)(没关系)我试过“干净”,但没有效果......我有之前导入另一个项目的 R 时出现类似错误,但这里没有导入 R...
我会很感激任何帮助,因为我被困在这里。!!
非常感谢
尼科
编辑:我的日志 => 在 mainactivity 有 CausedBy NUll 指针异常的地方:27,这是我在上面的代码中标记的行......
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)
和我的 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>
这是我的 pageAdapter 中的 InstantiateItem 方法:
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;
}
即使在这里,它也会抛出一个异常,而在我的两个布局中,checks_matin/soir,id nom/matricule 等都存在......