【问题标题】:Starting new activity from fragment starts a blank activity从片段开始新活动开始一个空白活动
【发布时间】:2017-01-25 19:53:33
【问题描述】:

我正在尝试为我的家乡制作一个基本的旅游应用程序。我在 listView 上设置了一个 onItemClickListener。我的目标是,当单击列表项时,我想打开一个新活动以向用户显示有关所选项目的更多信息(地址、电话号码、GPS 位置等)。我在发送意图之前设置了片段中的所有文本视图,因此,当我设置内容视图时,它应该显示已填充的所有准确信息。

问题是,当单击列表视图时,活动会启动一个空白活动。没有错误消息,并且 logCat 显示文本视图已正确更新,但它们只是没有正确显示布局。

我已经为此工作了几天,如果任何人都可以提供任何帮助,我将不胜感激。提前致谢。代码如下。

**这是包含要显示的信息的arrayList的片段。

public class FoodFragment extends Fragment {

public FoodFragment() {
    // Required empty public constructor
}

public TourSite currentSite;


@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.sites_listview, container, false);

    // Create an ArrayList of TourSite objects for main tab lists
    final ArrayList<TourSite> tourSites = new ArrayList<TourSite>();
    tourSites.add(new TourSite(R.string.ginasAddress, R.string.ginasNumber, R.string.ginasGPS,
            R.string.ginasWeblink, R.drawable.pizza_100, R.string.ginasName));
    tourSites.add(new TourSite(R.string.ninosAddress, R.string.ninosnumber, R.string.ninosGPS,
            R.string.ninosWeblink, R.drawable.pizza_100, R.string.ninosName));
    tourSites.add(new TourSite(R.string.topsAddress, R.string.topsDinerNumber,
            R.string.topsDinerGPS, R.string.topsWeblink, R.drawable.restaurant_100,
            R.string.topsName));
    tourSites.add(new TourSite(R.string.goldenEagleAddress, R.string.goldenEagleNumber,
            R.string.goldenEagleGPS, R.drawable.noodles_100, R.string.goldenEagleName));


    //Creates adapter that displays tourSites on tabs
    TourSiteListAdapter adapter = new TourSiteListAdapter(getActivity(), tourSites);

    //finds the listview that were using to display the images and names
    ListView listview = (ListView) rootView.findViewById(R.id.siteList);

    listview.setAdapter(adapter);


    //attaches Listener to listView in order to open new activity with further
    // information about the toursite
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            //finds the selected list item
            currentSite = tourSites.get(position);

            //inflates the layout we want to display using the intent below
            LayoutInflater inflater1 = (LayoutInflater) getContext().getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            View root = inflater1.inflate(R.layout.list_item_info, null, true);


            //finds the textViews in the layout that we want to update before
            // displaying in activity
            TextView addressView = (TextView) root.findViewById(R.id.addressView);
            TextView numberView = (TextView) root.findViewById((R.id.numberView));
            TextView gpsView = (TextView) root.findViewById(R.id.gpsView);
            TextView weblinkView = (TextView) root.findViewById(R.id.weblinkView);

            //some toursites dont have phone numbers or websites. This handles that
            // while also updateding the content to be displayed
            if (currentSite.hasAddress()) {
                addressView.setText(currentSite.getmAddress());

                addressView.setVisibility(View.VISIBLE);
            } else {
                addressView.setVisibility(View.GONE);
            }

            if (currentSite.hasPhone()) {
                numberView.setText(currentSite.getmPhone());

                numberView.setVisibility(View.VISIBLE);
            } else {
                numberView.setVisibility(View.GONE);
            }

            gpsView.setText(currentSite.getmGPSlink());

            gpsView.setMovementMethod(LinkMovementMethod.getInstance());

            if (currentSite.hasWeblink()) {
                weblinkView.setText(currentSite.getmWeblink());

                weblinkView.setVisibility(View.VISIBLE);
                weblinkView.setMovementMethod(LinkMovementMethod.getInstance());
            } else {
                weblinkView.setVisibility(View.GONE);
            }

            //used these logs to make sure the textviews were being updated correctly
            Log.d("addressView = ", addressView.getText().toString());
            Log.d("numberView = ", numberView.getText().toString());
            Log.d("gpsView = ", "" + gpsView.getText().toString());
            Log.d("weblinkView = ", weblinkView.getText().toString());

            //starts new activity that displays tourSite info
            Intent showInfo = new Intent(getContext(), TourSiteInfo.class);
            startActivity(showInfo);

        }
    });

    return rootView;
}

}

**这是应该显示布局的活动,但为空白

public class TourSiteInfo extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.list_item_info);
}
}

**这是应该显示的 XML 布局

<LinearLayout
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:orientation="vertical"
android:padding="24dp"
android:id="@+id/infoLayout">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="123 Main Ave, Newark, New Jersey"
android:id="@+id/addressView"
    android:padding="16dp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="123 Main Ave, Newark, New Jersey"
    android:id="@+id/numberView"
    android:padding="16dp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="123 Main Ave, Newark, New Jersey"
    android:id="@+id/gpsView"
    android:padding="16dp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="123 Main Ave, Newark, New Jersey"
    android:id="@+id/weblinkView"
    android:padding="16dp"/>

</LinearLayout>

***这是包含viewPager和tabLayout的mainActivity java

public class MainActivity extends AppCompatActivity {

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

    ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);

    CategoryAdapter adapter = new CategoryAdapter(this, getSupportFragmentManager());

    viewpager.setAdapter(adapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    tabLayout.setupWithViewPager(viewpager);
}
}

**mainActivity XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/mainLayout">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

***LogCat 应用程序启动并单击列表项时

01/25 14:37:31: Launching app
D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
D/addressView =: ​503 Frank E Rodgers Blvd N, Harrison, NJ 07029
D/numberView =: ​(973) 482-4883
D/gpsView =: ​(correct google link displays but stack overflow won't allow it)
D/weblinkView =: ​ginaspizzanj.com
I/Timeline: Timeline:Activity_launch_requestid
:com.example.android.tourguide
time:222869939
D/SecWifiDisplayUtil: Metadata value : none
D/ViewRootImpl: #1 mView =
com.android.internal.policy.
PhoneWindow$DecorView{d9020d4 I.E......  R.....ID 0,0-0,0}
D/ViewRootImpl: MSG_RESIZED_REPORT: 
ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
W/DisplayListCanvas: DisplayListCanvas is started on unbinded
RenderNode (without mOwningView)
I/Timeline: Timeline: Activity_idle id:
android.os.BinderProxy@5ab3627 time:222870123
V/ActivityThread: updateVisibility : ActivityRecord{5e11572
token=android.os.BinderProxy@3d0da23
{com.example.android.tourguide/com.example.android.tourguide.MainActivity}} show : true

我这几天一直在研究和尝试不同的解决方案,但没有任何效果。

【问题讨论】:

  • 每当您启动一个活动时,它就像一个新屏幕。您不能在启动之前为此视图设置某些内容。虽然您可以将游客参数作为意图包额外传递,然后在活动布局中设置这些值。
  • 谢谢阿努拉格!!你和克里斯蒂安都帮我解决了我的问题!

标签: java android android-fragments android-intent


【解决方案1】:

所以,我认为您需要在 TourSiteInfo 类中进行 UI 初始化。在onItemClick 中膨胀和设置值没有意义。它只会创建一个新视图,并在创建后不久将GCed,在那里创建的视图与TourSiteInfo 活动没有任何关系。

所以这是你需要做的。

  1. Intent showInfo = new Intent(getContext(), TourSiteInfo.class);。在此之后,您需要将所有站点信息设置为此意图作为额外值。可以参考this

  2. TourSiteInfo.onCreate 中的意图中提取信息并将值设置为控件。代码将与您在 onItemClick 中所做的完全相同。

【讨论】:

  • 下班后我要试试这个。谢谢你的建议。我会在尝试后更新。
  • 成功了!!非常感谢!!很高兴你告诉我捆绑包,因为我搜索的解决方案都没有提到它们。非常感谢!
猜你喜欢
  • 2018-04-11
  • 2015-02-19
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多