从片段开始新活动会启动空白活动

时间:2017-01-25 19:53:34

标签: java android android-fragments android-intent

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

问题是,单击列表视图时,活动会启动空白活动。没有错误消息,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);
}
}

** mainAcivity 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

我几天来一直在研究和尝试不同的解决方案,但还没有任何工作。

1 个答案:

答案 0 :(得分:0)

所以,我认为你需要在TourSiteInfo类中进行UI初始化。在onItemClick中充气和设置值并不合理。它只会创建一个新视图,并且会在创建后很快GC创建,在那里创建的视图与TourSiteInfo活动没有任何关系。

所以这就是你需要做的。

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

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

相关问题