【问题标题】:OnClickListener and fragments causing application crashOnClickListener 和 Fragments 导致应用程序崩溃
【发布时间】:2016-09-23 15:21:24
【问题描述】:

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

从阅读崩溃报告看来,错误发生在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);
}

}

【问题讨论】:

  • 在此处发布您的 xml
  • 我可能已经完成了,但我包含了相关的 xml
  • 有没有名为whatIsHumanTrafficing的片段?
  • 发布您的堆栈跟踪。
  • @indramurari 将其添加到底部

标签: android android-layout android-fragments fragmentmanager


【解决方案1】:

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

setContentView(R.layout.activity_main);

并且您正在同一个MainActivity 中访问whatIsHumanTrafficingnamed TextView,这是不可能的,因为它不存在于activity_main 中,而存在于另一个xml 中。所以你在那里得到NullPointerException

【讨论】:

  • 这很有意义。我是否必须从what is human trafficing 片段访问文本视图,因为它引用了正确的布局?然后在那个fragment中启动fragment事务?
  • @Davep 您可以访问textview,当且仅当您膨胀了相关的xml
  • 差不多。我们通过它的 id 访问文本视图。这存储在一个单独的文件中R.id.... 为什么必须对 xml 进行膨胀才能知道该文本视图存在?
  • 最初我们对SplashScreen 片段进行膨胀。这包含what is human trafficing 文本视图。所以我应该能够获得对该文本视图的引用,对吧?
  • @Davep whatIsHumanTrafficing textview 只能在 whatIsHumanTrafficing Fragment 中访问,而不能在 MainActivity 中访问
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-10
  • 2019-02-10
  • 1970-01-01
  • 2015-10-21
  • 2016-06-04
  • 2011-08-24
相关资源
最近更新 更多