无法在android中按下按钮时从片段加载意图

时间:2015-02-11 06:42:16

标签: java android android-intent android-fragments

我有几个片段,在我的Android应用程序中,片段加载正常,但我想通过按钮单击执行一个意图(按钮位于其中一个片段上)。这是代码。

MainActivity.java

public class MainActivity extends FragmentActivity{
ViewPager viewPager=null;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        viewPager= (ViewPager) findViewById(R.id.pager);
        button = (Button) findViewById(R.id.button);

        // Capture button clicks
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

                // Start NewActivity.class
                Intent myIntent = new Intent(MainActivity.this,eventload.class);
                startActivity(myIntent);
            }
        });
        FragmentManager fragmentManager=getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));
    }
}
class MyAdapter extends FragmentPagerAdapter
{
    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }
    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;
        if (i==0)
        {
            fragment = new FragmentA();
        }
        if (i==1)
        {
            fragment = new FragmentB();
        }
        if (i==2)
        {
            fragment = new FragmentC();
        }


        return fragment;
    }

    @Override
    public int getCount() {

        return 3;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        String title = new String();
        if (position==0)
        {
            return "Technical";
        }
        if (position==1)
        {
            return "Cultural";
        }
        if (position==2)
        {
            return "Sports";
        }


        return super.getPageTitle(position);
    }

}

这是eventload类

public class eventload extends Activity {

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.load);
    }

}

Load.xml只有一个单选按钮。

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.layouts"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity"></activity>
        <activity android:name=".eventload"></activity>
        <activity
            android:name=".SplashScreen"
            android:label="@string/app_name" >


            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

显示启动画面后,应用程序始终崩溃(启动画面不会干扰此处)。我的logcat说

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layouts/com.example.layouts.MainActivity}: java.lang.NullPointerException

我在清单中提到了MainActicity。

这是活动main.xml

<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:background="@drawable/ic_launcher">
   <android.support.v4.view.PagerTitleStrip
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />


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

按钮位于不同的片段中 frag4.xml

<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="com.example.layouts.MainActivity$PlaceholderFragment" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/rounded_corner_box" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="See events"
            android:id="@+id/button"
            android:layout_gravity="left|top" />
    </FrameLayout>

</RelativeLayout>

FragmentC.java

 public class FragmentC  extends Fragment{
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.frag4,container,false);
        }
    }

3 个答案:

答案 0 :(得分:1)

您告诉按钮位于片段上,但您正在实例化活动中不存在的活动中的按钮。所以它得到了NullPointerException。实例化按钮所在的片段中的按钮。

<强> UPDATE1 像这样

更改片段onCreateView()方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.frag4, container, false);
    Button button = (Button) view.findViewById(R.id.button);

    // Capture button clicks
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            // Start NewActivity.class
            Intent myIntent = new Intent(getActivity(), eventload.class);
            startActivity(myIntent);
        }
    });

    return view

答案 1 :(得分:1)

更改frag4的代码如下:

     public class FragmentC  extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
               View v = inflater.inflate(R.layout.frag4, container, false);
    // here v is the View you inflated from frag4.xml
    Button button = (Button) v.findViewById(R.id.button);

    // Capture button clicks
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            // Start NewActivity.class
            Intent myIntent = new Intent(getActivity(),eventload.class);
            startActivity(myIntent);
        }
    });
    return v;
    }
}

答案 2 :(得分:0)

在创建intent对象时将getApplicationContext()作为第一个参数传递。

现在您的button.setOnClickListener()应该是

button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            // Start NewActivity.class
            Intent myIntent = new Intent(getApplicationContext(),eventload.class);
            startActivity(myIntent);
        }
    });