使用 ActionBarSherlock 片段的按钮中的 NullPointerException setOnClickListener

NullPointerException setOnClickListener in a button using ActionBarSherlock fragments

我是 Android 开发的新手,我正在尝试使用 ABS 创建一个选项卡 activity 以便我可以将它与 +10 API 一起使用,我到目前为止成功。但是当我试图在一个片段中创建一个按钮来启动另一个 activity 我有一个 NullPointException 时,我的问题出现了。这是我的文件。
MainPaiement.java

package com.enhancedtechnologies.siclapplication.GestionPaiement;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.enhancedtechnologies.siclapplication.R;

public class MainPaiement extends SherlockFragmentActivity {

// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main_paiement);

    // Activate Navigation Mode Tabs
    mActionBar = getSupportActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Locate ViewPager in activity_main.xml
    mPager = (ViewPager) findViewById(R.id.pager);

    // Activate Fragment Manager
    FragmentManager fm = getSupportFragmentManager();

    // Capture ViewPager page swipes
    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // Find the ViewPager Position
            mActionBar.setSelectedNavigationItem(position);
        }
    };

    mPager.setOnPageChangeListener(ViewPagerListener);
    // Locate the adapter class called ViewPagerAdapter.java
    ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
    // Set the View Pager Adapter into ViewPager
    mPager.setAdapter(viewpageradapter);

    // Capture tab button clicks
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Pass the position on tab click to ViewPager
            mPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }
    };

    // Create first Tab
    tab = mActionBar.newTab().setText("Nouveau Paiement").setTabListener(tabListener);
    mActionBar.addTab(tab);

    // Create second Tab
    tab = mActionBar.newTab().setText("Paiement         Effectu\u00e9").setTabListener(tabListener);
    mActionBar.addTab(tab);

}
}

FragmentPaiementEffec.java

package com.enhancedtechnologies.siclapplication.GestionPaiement;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragment;
import com.enhancedtechnologies.siclapplication.R;


public class FragmentPaiementEffec extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {



    View view = inflater.inflate(R.layout.fragment_paiement_rech, container, false);

// button chercher
    Button btnchercher = (Button) view.findViewById(R.id.button6);

    // this is Line 30


    btnchercher.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), ActivityPaiementrechercheResultat.class));
        }
    });
    return view;

}
}

LogCat

07-27 20:30:32.264    1190-1190/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.enhancedtechnologies.siclapplication.GestionPaiement.FragmentPaiementEffec.onCreateView(FragmentPaiementEffec.java:29)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
        at android.view.View.measure(View.java:8313)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
        at android.view.View.measure(View.java:8313)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
        at android.view.View.measure(View.java:8313)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
        at android.view.View.measure(View.java:8313)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
        at android.view.View.measure(View.java:8313)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
        at android.view.View.measure(View.java:8313)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:130)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)

编辑

fragment_paiement_rech.xml##

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFF1E1" >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:id="@+id/linearLayout9"
            android:weightSum="1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Recherche des taxes recouvrées"
                android:id="@+id/textView20"
                android:textColor="#FF6D2C20"
                android:textSize="35sp"
                android:layout_marginBottom="35dp"
                android:layout_gravity="center_horizontal" />

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linearLayout4"
                android:layout_marginBottom="30dp"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Taxe"
                    android:id="@+id/textView15"
                    android:textColor="#FF6D2C20"
                    android:textSize="25sp"
                    android:layout_marginBottom="15dp" />

                <Spinner
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner2"
                    android:background="#ffffc391" />
            </LinearLayout>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linearLayout7"
                android:layout_marginBottom="30dp"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="CIN Redevable/Numéro de patente"
                    android:id="@+id/textView16"
                    android:textColor="#FF6D2C20"
                    android:textSize="25sp"
                    android:layout_marginBottom="15dp" />

                <EditText
                    android:layout_width="300dp"
                    android:layout_height="60dp"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/editText4"
                    android:background="#ffffc391" />
            </LinearLayout>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linearLayout8"
                android:layout_marginBottom="30dp"
                android:gravity="center_horizontal" >

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="30dp" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Du : "
                        android:id="@+id/textView18"
                        android:textColor="#FF6D2C20"
                        android:textSize="25sp"
                        android:layout_marginLeft="100dp" />

                    <EditText
                        android:layout_width="300dp"
                        android:layout_height="60dp"
                        android:inputType="numberDecimal"
                        android:ems="10"
                        android:id="@+id/editText5"
                        android:background="#ffffc391" />
                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Période de paiement"
                    android:id="@+id/textView17"
                    android:textColor="#FF6D2C20"
                    android:textSize="25sp"
                    android:layout_marginBottom="15dp" />

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Au : "
                        android:id="@+id/textView19"
                        android:textColor="#FF6D2C20"
                        android:textSize="25sp"
                        android:layout_marginLeft="100dp" />

                    <EditText
                        android:layout_width="300dp"
                        android:layout_height="60dp"
                        android:inputType="numberDecimal"
                        android:ems="10"
                        android:id="@+id/editText6"
                        android:background="#ffffc391" />
                </LinearLayout>
            </LinearLayout>

            <Button
                style="@style/styleButtons_port"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:text="Chercher"
                android:id="@+id/button2"
                android:textSize="35sp"
                android:padding="10dp"
                android:textColor="#ff000000" />

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

如果能得到任何帮助,我将不胜感激!!

您的应用程序似乎无法在视图堆栈中找到 button6。 确保您创建了这样的视图。 尝试调试并 wach 如果此对象在到达之前存在。

您在 fragment_redevable_rech 中定义了 button6,但 FragmentPaiementEffec 正在从 fragment_paiement_rech 创建其视图,这是一个不同的布局文件。你的片段应该膨胀 fragment_redevable_rech.

你的片段的 onCreateView 应该有

View view = inflater.inflate(R.layout.fragment_redevable_rech, container, false);