【问题标题】:Button not showing up on Activity that opens from Fragment,but button shows up in Graphical Layout按钮未显示在从片段打开的 Activity 上,但按钮显示在图形布局中
【发布时间】:2014-08-28 17:24:13
【问题描述】:

我在我的应用程序中创建了片段(6 个片段)。在我的第一个片段(android_frag)中有一个按钮“在地图上显示位置”。当我单击该按钮时,会打开新的活动名称“ShowPlaces”。在 show_places xml 文件中,我创建了一个按钮,该按钮应该让我返回上一个屏幕,即 android 片段,但当 ShowPlaces 活动打开时,按钮未显示,尽管我可以在 xml 文件的图形布局中看到该按钮。我一直无法找出问题所在。请帮忙。

这是我的 show_places.xml 文件

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


<!-- Show on Map button -->

<Button
    android:id="@+id/btn_show_map1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:text="@string/showplaces"
     />

  </LinearLayout>

这里是 ShowPlaces.java 文件

package com.example.tabswipe;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.app.Activity;
import android.view.Menu;


public class ShowPlaces extends Activity implements OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      View ios = inflater.inflate(R.layout.show_places, container, false);



      Button button = (Button) ios.findViewById(R.id.btn_show_map);
           button.setOnClickListener(new OnClickListener()
           {    @Override
               public void onClick(View v) {
                    // here you set what you want to do when user clicks your button,
                    // e.g. launch a new activity
              Intent openStartingPoint = new Intent("com.example.tabswipe.Android");
                  startActivity(openStartingPoint);

                }
           }); 
           return ios;


   }


 public void onClick(View v) {
  // TODO Auto-generated method stub

 }





  }

【问题讨论】:

    标签: java xml android-activity android-fragments android-button


    【解决方案1】:

    看起来您在 xml 中有 android:id="@+id/btn_show_map1" 但正在尝试查找 id btn_show_map。应该将这些更改为与另一个相同。

    【讨论】:

    • 感谢您的快速回复......是的......只是这样......发布问题时输入错误
    猜你喜欢
    • 2018-10-18
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多