【问题标题】:How to open new activity that contain RecyclerView and CardView?如何打开包含 RecyclerView 和 CardView 的新活动?
【发布时间】:2018-08-12 15:05:41
【问题描述】:

我创建了一个使用 RecyclerViewCardView(用于 PointOfInterest)的项目。这 5 项活动相互关联:

  1. PointOfInterest.java
  2. PlacesAdapter.java
  3. Places.java
  4. layout_poi.xml
  5. activity_point_of_interest.xml

同时在 activity_main.xml 中我设计了主菜单和一些按钮。其中一个名为 Rapid Penang 的按钮(id: rapid_btn)。我将 Rapid Penang 的一项活动(来自 MainActivity.java)称为如下:

public class MainActivity extends AppCompatActivity {

    private Button button_for_rapid;

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

        // to call Rapid Penang class
        button_for_rapid = (Button) findViewById(R.id.rapid_btn);
        button_for_rapid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openRapid();
            }
        });
    }

    public void openRapid()
    {
        Intent intent_rapid = new Intent(this, RapidPenang.class);
        startActivity(intent_rapid);
    }

}

RapidPenang 只有一项活动,它是成功的。但是当我尝试对 PointOfInterest 活动(如上所述)执行完全相同的操作时,应用程序突然崩溃了。

这就是我尝试从 MainMenu 中名为 Point Of Interest 的按钮打开 PointOfInterest 活动的方式:

private Button button_for_poi;

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

    // to call Point Of Interest class
    button_for_poi = (Button) findViewById(R.id.poi_btn);
    button_for_poi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openPOI();
        }
    });
}

public void openPOI()
{
    Intent intent_poi = new Intent(this, PointOfInterest.class);
    Intent intent_poi2 = new Intent(this, PlacesAdapter.class);
    Intent intent_poi3 = new Intent(this, Places.class);
    startActivity(intent_poi);
}

【问题讨论】:

  • 请显示Logcat中显示的内容
  • "...包含 RecyclerView 和 CardView" - 这与启动 Activity 有什么关系。它只需要和 Intent 来启动一个 Activity(不管它包含什么)。

标签: java android android-recyclerview


【解决方案1】:

首先检查您的活动是否在 Android 清单文件中定义,然后调用

            StartActivity(new Intent(getApplicationcontext(),RapidPenang.class));

就是这样

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多