gpt4 book ai didi

java - 在 android 中单击按钮时无法从 fragment 加载 Intent

转载 作者:行者123 更新时间:2023-12-02 04:59:19 26 4
gpt4 key购买 nike

我的 fragment 很少,在我的 Android 应用程序中, fragment 加载得很好,但我想通过单击按钮来执行 Intent (按钮位于其中一个 fragment 上)。这是代码。

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);
}

}

这是事件加载类

public class eventload extends Activity {

Button button;

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

}

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

这是 list

<?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

我在 list 中提到了 MainActicity。

这是 Activity 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>

按钮位于不同的 fragment 中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);
}
}

最佳答案

修改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;
}
}

关于java - 在 android 中单击按钮时无法从 fragment 加载 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28448081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com