MainActivity setOnClickListener中的java.lang.NullPointerException

MainActivity setOnClickListener中的java.lang.NullPointerException,java,android,nullpointerexception,Java,Android,Nullpointerexception,当我运行我的应用程序时,我在第56行不断得到一个NullPointerException,如下面的调试日志所示。我对android应用程序的编程比较陌生,所以我不知道这里发生了什么。有人能帮我解决这个问题吗? My MainActivity.java: package com.termite.pockettube; import java.util.Locale; import android.content.Intent; import android.os.Bundle; import

当我运行我的应用程序时,我在第56行不断得到一个NullPointerException,如下面的调试日志所示。我对android应用程序的编程比较陌生,所以我不知道这里发生了什么。有人能帮我解决这个问题吗? My MainActivity.java:

package com.termite.pockettube;

import java.util.Locale;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements View.OnClickListener{

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

     Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this); //this is line 56
    }
    public void countryOnClick() {
        startActivity(new Intent("com.termite.pockettube.COUNTRY"));
    }
    @Override
public void onClick(View v) {
    switch (v.getId()){
    case R.id.button:
        countryOnClick();
        break;
    }
}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            switch (position) {
            case 0:
                Fragment1 fragment1 = new Fragment1();
                return fragment1;
            case 1:
                Fragment2 fragment2 = new Fragment2();
                return fragment2;
            case 2:
                Fragment3 fragment3 = new Fragment3();
                return fragment3;
            case 3:
                Fragment4 fragment4 = new Fragment4();
                return fragment4;
            case 4:
                Fragment5 fragment5 = new Fragment5();
                return fragment5;
            }
            return null;
        }

        @Override
        public int getCount() {
            // Show 5 total pages.
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            case 3:
                return getString(R.string.title_section4).toUpperCase(l);
            case 4:
                return getString(R.string.title_section5).toUpperCase(l);
            }
            return null;
        }
    }

}
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
这是我尝试运行应用程序时的调试日志:

12-20 01:13:26.898: D/AndroidRuntime(6539): Shutting down VM
12-20 01:13:26.906: W/dalvikvm(6539): threadid=1: thread exiting with uncaught exception (group=0x40af21f8)
12-20 01:13:26.906: E/AndroidRuntime(6539): FATAL EXCEPTION: main
12-20 01:13:26.906: E/AndroidRuntime(6539): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.termite.pockettube/com.termite.pockettube.MainActivity}: java.lang.NullPointerException
12-20 01:13:26.906: E/AndroidRuntime(6539):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2060)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1181)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at android.os.Looper.loop(Looper.java:137)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at android.app.ActivityThread.main(ActivityThread.java:4558)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at java.lang.reflect.Method.invokeNative(Native Method)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at java.lang.reflect.Method.invoke(Method.java:511)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at dalvik.system.NativeStart.main(Native Method)
12-20 01:13:26.906: E/AndroidRuntime(6539): Caused by: java.lang.NullPointerException
12-20 01:13:26.906: E/AndroidRuntime(6539):     at com.termite.pockettube.MainActivity.onCreate(MainActivity.java:56)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at android.app.Activity.performCreate(Activity.java:4635)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-20 01:13:26.906: E/AndroidRuntime(6539):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1999)
12-20 01:13:26.906: E/AndroidRuntime(6539):     ... 11 more
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
清单xml代码:

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml:

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" 
         >

    </android.support.v4.view.PagerTabStrip>



</android.support.v4.view.ViewPager>

fragment1.xml:

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$DummySectionFragment" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:text="@string/button" />

</RelativeLayout>

检查
R.id.button
,按钮是否在
R.layout.activity\u main
布局中

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
发布您的
活动\u main.xml
布局

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
做下面的事

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
activity\u main.xml
中添加按钮或更改代码

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
button = (Button) mViewPager.findViewById(R.id.button);
button.setOnClickListener(this);
按钮为空。初始化失败

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
activity\u main.xml
中需要按钮,id也应该是按钮

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
findViewById
在当前展开的布局中查找id为的视图。所以,如果您在
activity\u main.xml
中有按钮,那么您的初始化工作正常,否则您将获得NPE`

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
activity\u main.xml
中没有按钮。S0
button=(button)findViewById(R.id.button)失败

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

按钮处于碎片1中。因此,您需要膨胀布局
fragment1.xml
,然后在
onCreateView
中初始化按钮,而不是在Activity中初始化按钮。

您需要在
Activity\u main.xml
中按下按钮,因为您在
fragment\u 1.xml
中添加了该按钮,这就是NPE的原因

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
activity\u main.xml中添加
按钮将解决您的问题

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
**activity_main.xml

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/padding_Small"
    android:shrinkColumns="*"
    android:stretchColumns="*" >

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pager_title_strip"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:textColor="#fff" />
    </android.support.v4.view.ViewPager>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="ButtonText" />

</RelativeLayout>

试试看

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
立即创建

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
然后试试看

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
private OnClickListener buttonListener = new OnClickListener() {
    public void onClick(View v) {
        //Do Work Here
    }
};

您的错误在getItem(Position)方法中。即使您的适配器大小是5,它也会为您提供比5更大的位置。在这样的时刻,从那里返回null

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

您可以通过添加一条Log语句来检查这一点,在getItem方法的开头打印位置值

谢谢大家的输入,所有这些都可以结束NPE,但它没有执行我希望它完成的任务。我将按钮添加到y Fragment1.java文件中,然后它就工作了

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.termite.pockettube.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.termite.pockettube.CountryActivity"
            android:label="@string/title_activity_country" >
            <intent-filter>
                <action android:name="com.termite.pockettube.COUNTRY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
package com.termite.pockettube;

import com.termite.pockettube.MainActivity.SectionsPagerAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class Fragment1 extends Fragment implements View.OnClickListener {
 Button button;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Get the view from fragmenttab1.xml
        View view = inflater.inflate(R.layout.fragment1, container, false);
        button = (Button)view.findViewById(R.id.button);
        button.setOnClickListener(this);
        return view;
    }
   /* @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }*/
    public void countryOnClick() {
        startActivity(new Intent("com.termite.pockettube.COUNTRY"));
    }
    @Override
public void onClick(View v) {
    switch (v.getId()){
    case R.id.button:
        countryOnClick();
        break;
    }
}
}

什么是第56行MainActivity.java?我认为按钮在片段中,所以在适配器中设置click listener。你不能在这里找到它,可能是按钮有问题显示你的xml代码,这将很容易找到错误。检查xml文件中的按钮id。它与“按钮”相同吗?这是否给出了
NullPointerException
?这是一个NPE。因此他的一个视图可能是空的。不是因为intnet。你能解释一下你的解决方案吗!?nullpointerexception当对象为Null时发生,我认为您必须首先定义AOObject,然后使用其方法,您可以通过这种方式进行测试,我希望这种方式可以帮助您。欢迎@saeid,这个答案与您之前的其他答案一样。你的答案有什么不同?