0

ボタンを押したときに新しいビューを読み込もうとしています。私はviewPagerを持っていて、ボタンはそれらのビューにあります。onClickを呼び出すと、アプリが起動しません。何かが起こる前にクラッシュします。私はこのバグを1週間以上潰そうとしてきましたが、これは非常にアマチュアなものだと思います。学生プログラマーを助けてください!

public class Main extends Activity {

    Button listButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.splash);

        SplashPager adapter = new SplashPager();
        ViewPager myPager = (ViewPager) findViewById(R.id.splashPager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);

        listButtonListener();
    }

    public void listButtonListener() {

        listButton = (Button) findViewById(R.id.splashB);
        listButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                setContentView(R.layout.list_layout);

            }

        });

    }

}

ログ:

01-16 20:09:49.149: W/dalvikvm(29035): threadid=1: thread exiting with uncaught exception         (group=0x40207560)
01-16 20:09:49.149: E/AndroidRuntime(29035): FATAL EXCEPTION: main
01-16 20:09:49.149: E/AndroidRuntime(29035): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.survtest2/com.example.survtest2.Main}: java.lang.NullPointerException
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.os.Looper.loop(Looper.java:130)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread.main(ActivityThread.java:3733)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at java.lang.reflect.Method.invokeNative(Native Method)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at java.lang.reflect.Method.invoke(Method.java:507)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at dalvik.system.NativeStart.main(Native Method)
01-16 20:09:49.149: E/AndroidRuntime(29035): Caused by: java.lang.NullPointerException
01-16 20:09:49.149: E/AndroidRuntime(29035):    at com.example.survtest2.Main.listButtonListener(Main.java:36)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at com.example.survtest2.Main.onCreate(Main.java:30)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-16 20:09:49.149: E/AndroidRuntime(29035):    ... 11 more
01-16 20:09:49.149: E/AndroidRuntime(29035): [Blue Error Handler] Make Debugging Report file for main
01-16 20:09:49.149: E/AndroidRuntime(29035): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.survtest2/com.example.survtest2.Main}: java.lang.NullPointerException
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.os.Looper.loop(Looper.java:130)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread.main(ActivityThread.java:3733)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at java.lang.reflect.Method.invokeNative(Native Method)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at java.lang.reflect.Method.invoke(Method.java:507)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at dalvik.system.NativeStart.main(Native Method)
01-16 20:09:49.149: E/AndroidRuntime(29035): Caused by: java.lang.NullPointerException
01-16 20:09:49.149: E/AndroidRuntime(29035):    at com.example.survtest2.Main.listButtonListener(Main.java:36)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at com.example.survtest2.Main.onCreate(Main.java:30)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-16 20:09:49.149: E/AndroidRuntime(29035):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-16 20:09:49.149: E/AndroidRuntime(29035):    ... 11 more

Spray.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/splashPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager>

</LinearLayout>

スプラッシュ2.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/intro__1" />

<Button
    android:id="@+id/splashB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_marginLeft="75dp"
    android:text="aaaaaaaaaaaaaaaaaa" />

</FrameLayout>

ViewPager:

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 4;
}

@Override
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.splash2;
        break;
    case 1:
        resId = R.layout.splash3;
        break;
    case 2:
        resId = R.layout.splash4;
        break;
    case 3:
        resId = R.layout.splash5;
        break;

    }

    View view = inflater.inflate(resId, null);
    ((ViewPager) collection).addView(view, 0);
    return view;

}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    // TODO Auto-generated method stub
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public Parcelable saveState() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    // TODO Auto-generated method stub
    return arg0 == ((View) arg1);
}

}
4

3 に答える 3

1

listButton.setOnClickListenerでの失敗に基づいて、R.id.splashBがレイアウトに見つからないと推測しています。

public class Main extends Activity {

Button listButton;
View splash2;  // Added

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    setContentView(R.layout.splash);
    SplashPager adapter = new SplashPager();
    ViewPager myPager = (ViewPager) findViewById(R.id.splashPager);
    ....
    LayoutInflater inflater = getLayoutInflater(); // Added
    splash2 = inflater.inflate (R.layout.splash2, null); // Added
    myPager.add (splash2); // Added

    listButtonListener();
}

public void listButtonListener() {

    listButton = (Button) splash2.findViewById(R.id.splashB); // Changed
    listButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            setContentView(R.layout.list_layout);
        }
    });
}
于 2013-01-17T01:29:20.100 に答える
0

NullPointerExceptionおそらく、であるために、をlistButton取得しますnull。レイアウトsplashBに実際に存在することを確認してください。splash

それが答えではない場合は、ブレークポイントを設定して、listButtonListener()スローされる理由を理解してNullPointerExceptionください。

于 2013-01-17T01:23:33.933 に答える
-1

削除してみてください

myPager.setCurrentItem(0);

デフォルトではViewPagerのデフォルトページは0であるため、ここではこれは必要ありません。このようにして、Androidシステムで作成されていない可能性のあるページを表示しようとしています。

編集:

public class Main extends Activity {

Button listButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.splash);

    SplashPager adapter = new SplashPager(this); // PASSING MAIN ACTIVITY FOR SETTING CONTENTVIEW
    ViewPager myPager = (ViewPager) findViewById(R.id.splashPager);
    myPager.setAdapter(adapter);
}...}

//アダプタコード

@Override
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.splash2;
        break;
    case 1:
        resId = R.layout.splash3;
        break;
    case 2:
        resId = R.layout.splash4;
        break;
    case 3:
        resId = R.layout.splash5;
        break;

    }

    View view = inflater.inflate(resId, null);
    if(resId == R.layout.splash2) {
       Button b = (Button) view.findViewById(R.id.splashB);
       b.setOnClickListner(new OnClickListener() {
       @Override
        public void onClick(View arg0) {
            // mainActivity is Activity variable passed in Adapter constructor
            mainActivity.setContentView(R.layout.list_layout);

        }
       });
    }
    ((ViewPager) collection).addView(view, 0);
    return view;

}
于 2013-01-17T01:26:44.570 に答える