OnClickListener和片段导致应用程序崩溃

时间:2016-09-23 15:21:24

标签: android android-layout android-fragments fragmentmanager

我正在尝试使用片段以编程方式更改屏幕上的文本。为此,我在文本视图上设置了一个on click侦听器,然后如果单击它启动一个片段管理器,则用新片段替换当前片段。但是,这会导致我的应用程序在启动时崩溃。

通过阅读崩溃报告,似乎错误发生在tv1.setOnClickLIstener ...

最后,当我使用片段或支持片段时,Android Studio不断给我一个类型不匹配。这就是您看到android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

的原因

Java代码:

public class MainActivity extends AppCompatActivity {


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

    //begin transaction
     android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    //replace the contents of the container with the new fragment
    ft.replace(R.id.placeHolder, new SplashScreenFragment());

    ft.commit();


    TextView tv1 = (TextView) findViewById(R.id.whatIsHumanTrafficing);
    tv1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

            ft.replace(R.id.placeHolder, new whatIsHumanTrafficing());
            ft.commit();
        }
    });

}

}
来自activity_main的

XML:

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

<FrameLayout
    android:id="@+id/placeHolder"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


</FrameLayout>

点击前显示的XML:

<?xml version="1.0" encoding="utf-8"?>
<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.piatt.worksafe.MainActivity"
android:id="@+id/splashScreenId"
>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Work Safe!"
    android:textSize="36sp"
    android:layout_centerHorizontal="true"
    android:paddingBottom="32dp"
    android:id="@+id/title"
     />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="What is Human Trafficing?"
    android:layout_below="@+id/title"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:padding="16dp"
    android:id="@+id/whatIsHumanTrafficing"
    android:clickable="true"

    />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="How do I get safe labor?"
    android:layout_below="@+id/whatIsHumanTrafficing"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:padding="16dp"
    android:id="@+id/howDoIGetSafeLabor"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="How do I check that my job / job offer is legal?"
    android:layout_below="@+id/howDoIGetSafeLabor"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:padding="16dp"
    android:gravity="center"
    android:id="@+id/checkLegality"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="How can I get help?"
    android:layout_below="@+id/checkLegality"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:padding="16dp"
    android:id="@+id/getHelp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="About us"
    android:layout_below="@+id/getHelp"
    android:layout_centerHorizontal="true"
    android:textSize="16sp"
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:id="@+id/aboutUs"
    />



 </RelativeLayout>

单击文本视图后显示的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">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Human Trafficing Is:"
    android:textSize="36sp"
    android:layout_centerHorizontal="true"
    android:paddingBottom="32dp"
    android:id="@+id/title"
    android:layout_gravity="center"
    />



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Confiscation of travel documents"
    android:layout_below="@+id/HTdescription1"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:padding="16dp"
    android:id="@+id/HTdescription1"
    android:layout_gravity="center"


    />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Unregistered Labor"
    android:layout_below="@+id/HTdescription1"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:padding="16dp"
    android:id="@+id/HDdescription2"
    android:layout_gravity="center"

    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Isolation from friends and family"
    android:layout_below="@+id/HTdescription2"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:padding="16dp"
    android:id="@+id/HDdescription3"
    android:layout_gravity="center"

    />



</LinearLayout>

片段命名为人口贩卖,我正试图膨胀:

public class whatIsHumanTrafficing  extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
    //inflate the layout for this fragment
    return inflater.inflate(R.layout.what_is_human_trafficing, container, false);
}

}

1 个答案:

答案 0 :(得分:0)

您在activity_main中使用了MainActivity,如下所示

setContentView(R.layout.activity_main);

并且您正在同一个whatIsHumanTrafficing中访问名为TextView的{​​{1}}这是不可能的,因为MainActivity中不存在,而activity_main中存在xml }}。所以你得到NullPointerException