我是Android新手,并试图自己学习.但我正在与Fragments度过艰难时期.我正在创建一个简单的应用程序来学习片段.我觉得这看起来很傻但我真的无法让它发挥作用.
我想要做的只是点击Fragment_One中的按钮(buttonSayHi),Fragment_One应该被Fragment_Two取代.
我不确定何时调用Fragment代码以及我应该在哪里编写代码来调用第二个片段.我收到错误:无法启动Activity组件.
但是,如果我丢弃按钮的监听器并且片段显示在活动中,则代码可以正常工作.
我做了大量的研究,阅读developer.android.com上的片段教程以及Lars Vogella的教程.我认为我的概念并不清楚.
任何帮助将不胜感激.
以下是代码:
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayoutFragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editTextPersonName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/buttonSayHi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Say Hi"
android:onClick="onButtonClicked"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I will say Hi!" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
package com.example.fragmenttutorial;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import …Run Code Online (Sandbox Code Playgroud) 我正在尝试加载列表RecyclerView并将列表的第一行显示为已选中.我使用以下代码实现了它:
@Override
public void onBindViewHolder(NavigationDrawerAdapter.ViewHolder holder, final int position) {
if (!mNavClassrooms.get(position).equals("")) {
holder.mTextViewClassroom.setText(mNavClassrooms.get(position)); // Setting the Text with the array of our Titles
holder.mRelLayClassroom.setSelected(mSelectedItems.get(position, false));
/*
The following code was written to make the first item in the Classroom list as selected.
It leads to the item always being selected and hence has been commented out.
*/
if(position == 0 && intOldSelectedItem == -1){
holder.mRelLayClassroom.setSelected(mSelectedItems.get(position, true));
intOldSelectedItem = 0;
mSelectedView = holder.mRelLayClassroom.getChildAt(position);
mSelectedItems.put(position, true);
}
else{
holder.mRelLayClassroom.setSelected(mSelectedItems.get(position, …Run Code Online (Sandbox Code Playgroud) 我正在使用MPAndroid库在Android应用程序中显示一个简单的条形图。条形图有时会完美显示数据。但是,有时即使数据集包含数据,它也会显示消息“没有可用的图表数据”。
仅当我点击“图表”区域时,才会显示该图表。我用谷歌搜索,但是找不到解决方案。以下是代码:
if (mCount > 0){mBarDataSet = new BarDataSet(mBarEntryAssessmentList, "Assessment Count");
mBarDataSet.setBarSpacePercent(5f);
mBarData = new BarData(trimmedSubjectNameList, mBarDataSet);
mBarData.setValueFormatter(new BarEntryValueFormatter()); // Setting a Value formatter to show Integer data instead of Float
mBarChart.setData(mBarData);
mBarChart.setDescription("");
mBarChart.setDrawGridBackground(false);
mBarChart.setDragEnabled(true);
mBarChart.setTouchEnabled(true);
mBarChart.setClickable(true);
mBarChart.setScaleXEnabled(false);
mBarChart.setScaleYEnabled(false);
mBarChart.setVisibleXRange(1, 4);
mBarChart.setHighlightPerDragEnabled(false);
mBarChart.setHighlightPerTapEnabled(true); // set this to true if we want to listen to click events
mBarChart.setOnChartValueSelectedListener(StudentProgressActivity.this);
XAxis xAxis = mBarChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawLabels(true);
xAxis.setDrawGridLines(false);
xAxis.setLabelsToSkip(0); // Shows all the labels as initially we had problems showing all the labels
YAxis …Run Code Online (Sandbox Code Playgroud)