ViewPager nullPointerException错误

时间:2014-03-04 17:55:27

标签: java android android-viewpager

我的主要活动

 package com.std.stunduizmainas;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;


import org.json.JSONArray;
import org.json.JSONObject;



import com.std.adapter.SSS;
import com.std.adapter.TabsPagerAdapter;
import com.std.stunduizmainas.R;





import android.os.AsyncTask;

import android.os.Bundle;
import android.os.StrictMode;





import android.app.ActionBar;
import android.app.ActionBar.Tab;

import android.app.AlertDialog;
import android.app.Dialog;



import android.app.FragmentTransaction;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;

import android.support.v4.app.FragmentActivity;

import android.support.v4.view.ViewPager;

import android.util.Log;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;

import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Spinner;

import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;






public class MainActivity extends FragmentActivity implements  ActionBar.TabListener {
public static String izm;

        private ViewPager viewPager;
        private TabsPagerAdapter mAdapter;
        private ActionBar actionBar;
       SharedPreferences settings; 
       Spinner skoluSp;
       Spinner klasuSp;
       Spinner izvSp;
       static String info="";
       static String rinfo="";
       Dialog fb;

       static String sd="";
       static String rd="";
       List<String> ssk = new ArrayList<String>();
        List<String> person = new ArrayList<String>();
        List<String> skl = new ArrayList<String>();
        List<String> tabi = new ArrayList<String>();
        String[] techerorstudent = new String[]{"Skolēns","Skolotājs"};
         static String[] tabs = new String[4];

    static String regid="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.enableDefaults();
        getActionBar().setTitle("Stundu");
        getActionBar().setSubtitle("izmainas");
        settings = this.getSharedPreferences("settings", Context.MODE_PRIVATE);
        getTab();
        startService(new Intent(this,SSS.class));

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        //tabs
        for (String tab_name : tabi) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }



        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {

                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

和我的TabsPagerAdapter

package com.std.adapter;



import com.std.stunduizmainas.KlasesIzm;
import com.std.stunduizmainas.RitdienasIzm;
import com.std.stunduizmainas.SkolasIzm;
import com.std.stunduizmainas.Paz;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new SkolasIzm();
        case 1:
            // Games fragment activity
            return new RitdienasIzm();

        case 2:
            // Movies fragment activity
            return new Paz();

        case 3:
            return new KlasesIzm(); 
        }

        return null;
    }



    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 4;
    }

}               

            }); 
        }

我似乎无法弄清楚问题是什么。我知道这是代码viewPager.setAdapter(mAdapter);。 log cat将此显示为错误:

03-04 17:45:48.092: E/AndroidRuntime(1880): FATAL EXCEPTION: main
03-04 17:45:48.092: E/AndroidRuntime(1880): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.std.stunduizmainas/com.std.stunduizmainas.MainActivity}: java.lang.NullPointerException
03-04 17:45:48.092: E/AndroidRuntime(1880):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at android.os.Looper.loop(Looper.java:137)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at java.lang.reflect.Method.invoke(Method.java:525)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at dalvik.system.NativeStart.main(Native Method)
03-04 17:45:48.092: E/AndroidRuntime(1880): Caused by: java.lang.NullPointerException
03-04 17:45:48.092: E/AndroidRuntime(1880):     at com.std.stunduizmainas.MainActivity.onCreate(MainActivity.java:132)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at android.app.Activity.performCreate(Activity.java:5133)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-04 17:45:48.092: E/AndroidRuntime(1880):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-04 17:45:48.092: E/AndroidRuntime(1880):     ... 11 more

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您的viewPager似乎是null

确保此行中使用的ID:

viewPager = (ViewPager) findViewById(R.id.pager);

有效,并引用activity_main.xml布局中的viewpager。