0

単純な問題のように見えるので、気が狂ってしまう問題があります... 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: 27 で CausedBy NUll Pointer Exception がある場所、これは上記のコードでマークした行です... 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などが存在します...

4

3 に答える 3

0

複数の xml レイアウトがありますか? その場合、findViewById がこのリソースを間違った xml で探している可能性があります。

于 2012-07-15T18:27:03.290 に答える
0

OKここでいくつかの便利なリンクを見つけました: Android ViewPager findViewById not working - Always return null

これが私の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" />

私はテストするためにインクルードにコメントしました...そしてここで私の新しいJavaコードを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());

しかし、それでも機能しません!!! 私はここでアイデアがありません... :/

編集:それはうまくいきます!! 上記の行を削除してください:

LinearLayout v1 = (LinearLayout)view.findViewById(R.id.linear);
于 2012-07-15T19:23:56.180 に答える
0

テキスト ビューR.id.nomは、使用するアクティビティにマップする xml に存在しませんsetContentView(R.layout.activity_main);

そのため、それを参照しようとするとTextView tv = (TextView) findViewById(R.id.nom);、コンパイラは activity_main.xml ファイルを調べるため、nullPointerException が発生します。

IDは別のxmlで定義されているため、R.javaファイルにマップ/存在します。

于 2012-07-15T18:10:49.690 に答える