【问题标题】:Android ListView, start new activityAndroid ListView,开始新的活动
【发布时间】:2011-09-07 18:07:00
【问题描述】:

嘿,谁能告诉我如何通过按下列表视图中的项目来开始活动?

这是我的预感:

编辑- 我已经修复了它,我认为因为我没有收到错误消息。但是当我开始这个活动(视频)时,应用程序会崩溃并想要强制关闭有什么问题?请帮助:D

这里是源代码-

包 com.alpha.liveshit;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Videos extends ListActivity {

String[] elements = {"video", "menu"};

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

      setContentView(R.layout.videos); 
     ListView listview = (ListView)findViewById(R.id.listView1);
     listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, elements));
 }

public void onListItemClick(ListView parent, View v, int position, long id) {

    if ("video".equals(elements[position]))
        {Intent myIntent = new Intent(Videos.this, Lars.class);
        startActivity(myIntent);}
    else if ("menu".equals(elements[position]))
        {Intent myIntent = new Intent(Videos.this, MenuActivity.class);
        startActivity(myIntent);}
    }
}

【问题讨论】:

    标签: android listview android-activity new-operator


    【解决方案1】:

    确保将活动添加到 Android 清单 在您的清单文件中,您应该像这样添加活动:

    <activity android:name=".Lars"/>
    

    确保为您计划使用的每个 Intent 和活动执行此操作。

    【讨论】:

      【解决方案2】:

      您将 Activity 作为参数传递给 startActivity() 函数

      startActivity(MenuActivity.class)
      

      相反,您应该像这样传递一个 Intent

      Intent myIntent = new Intent(Videos.this, MenuActivity.class);
      startActivity(myIntent);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多