Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Studio-如何从列表视图打开特定活动?_Java_Android_Listview_Android Studio - Fatal编程技术网

Java Android Studio-如何从列表视图打开特定活动?

Java Android Studio-如何从列表视图打开特定活动?,java,android,listview,android-studio,Java,Android,Listview,Android Studio,我在完成代码以打开列表中的活动时遇到问题。我正在制作一个方程式应用程序,我想有一个主题列表,当你点击其中一个时,它会启动包含我已经拥有的方程式的.xml文件。我已经将活动绑定到java类 这是我的密码: MainActivity.java: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layou

我在完成代码以打开列表中的活动时遇到问题。我正在制作一个方程式应用程序,我想有一个主题列表,当你点击其中一个时,它会启动包含我已经拥有的方程式的.xml文件。我已经将活动绑定到java类

这是我的密码:

MainActivity.java:

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

    //get list view from xml
    temasListView = (ListView) findViewById(R.id.temasListView);


    String[] Temas = {
            "Conversion",
            "Suma",
            "Trigonometria",
            "Primera",
            "Momento",
            "Centro",
            "Segunda1",
            "MRU",
            "MRUA",
            "Tiro",
            "Segunda2"};

    ListAdapter temasAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Temas);
    ListView temasListView = (ListView) findViewById(R.id.temasListView);
    temasListView.setAdapter(temasAdapter);

    temasListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String temas = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(Temas.this, temas, Toast.LENGTH_LONG).show();

                    if (position == 0) {
                        Intent intent = new Intent(this, Conversion.class);
                        startActivity(intent);
                    }
                    else if (position == 1) {
                        Intent intent = new Intent(this, Suma.class);
                        startActivity(intent);
                        }
                    else if (position == 2) {
                        Intent intent = new Intent(this, Trigonometria.class);
                        startActivity(intent);
                    }
                    else if (position == 3) {
                        Intent intent = new Intent(this, Primera.class);
                        startActivity(intent);}

                    else if (position == 4) {
                        Intent intent = new Intent(this, Momento.class);
                        startActivity(intent);
                    }
                    else if (position == 5) {
                        Intent intent = new Intent(this, Centro.class);
                        startActivity(intent);
                    }
                    else if (position == 6) {
                        Intent intent = new Intent(this, Segunda1.class);
                        startActivity(intent);
                    }

                    else if (position == 7) {
                        Intent intent = new Intent(this, MRU.class);
                        startActivity(intent);
                    }
                    else if (position == 8) {
                        Intent intent = new Intent(this, MRUA.class);
                        startActivity(intent);
                    }
                    else if (position == 9) {
                        Intent intent = new Intent(this, Tiro.class);
                        startActivity(intent);
                    }
                    else if (position == 10) {
                        Intent intent = new Intent(this, Segunda2.class);
                        startActivity(intent);
                    }




            });
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
//从xml获取列表视图
temasListView=(ListView)findViewById(R.id.temasListView);
字符串[]Temas={
“转换”,
“相扑”,
“三角学”,
“Primera”,
“Momento”,
“中心”,
“Segunda1”,
“MRU”,
“MRUA”,
“提罗”,
“Segunda2”};
ListAdapter temasAdapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,Temas);
ListView temasListView=(ListView)findViewById(R.id.temasListView);
setAdapter(temasAdapter);
temasListView.setOnItemClickListener(
新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
String temas=String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(Temas.this,Temas,Toast.LENGTH_LONG.show();
如果(位置==0){
Intent Intent=新的Intent(这个,Conversion.class);
星触觉(意向);
}
否则如果(位置==1){
意向意向=新意向(此,Suma.class);
星触觉(意向);
}
否则如果(位置==2){
意向意向=新意向(本,三角函数类);
星触觉(意向);
}
否则如果(位置==3){
意向意向=新意向(此,Primera.class);
星触觉(意图);}
否则如果(位置==4){
意向意向=新意向(此,Momento.class);
星触觉(意向);
}
否则如果(位置==5){
意向意向=新意向(本,中心类);
星触觉(意向);
}
否则如果(位置==6){
意向意向=新意向(此,Segunda1.class);
星触觉(意向);
}
否则如果(位置==7){
意向意向=新意向(此,MRU.class);
星触觉(意向);
}
否则如果(位置==8){
意向意向=新意向(此,MRUA.class);
星触觉(意向);
}
否则如果(位置==9){
意向意向=新意向(本,Tiro.class);
星触觉(意向);
}
否则如果(位置==10){
意向意向=新意向(此,Segunda2.class);
星触觉(意向);
}
});
}
strings.xml:

<resources>
<string name="app_name"></string>

<string-array name="temas">
    <item>Conversión de Unidades</item>
    <item>Suma de Vectores</item>
    <item>Trigonometría</item>
    <item>Primera Ley de Newton</item>
    <item>Momento de Fuerzas</item>
    <item>Centro de Gravedad</item>
    <item>Componente de Velocidad</item>
    <item>Segunda Ley de Newton</item>
    <item>Movimiento Rectilíneo Uniforme</item>
    <item>MRUA</item>
    <item>Tiro Vertical</item>
    <item>Segunda Ley de Newton (DCL)</item>


</string-array>

大学转化
矢量相册
三角函数
牛顿总理
弗尔扎斯时刻酒店
格拉维达德中心酒店
速度成分
塞贡达莱德牛顿
直发运动制服
MRUA
提罗垂直
Segunda Ley de Newton(DCL)

我的主要活动是:

<LinearLayout   
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:entries="@array/temas"
    android:id="@+id/temasListView"
    android:layout_weight="1.05"
    android:background="#dadada" />

</LinearLayout>
将此行从

Toast.makeText(Temas.this, temas, Toast.LENGTH_LONG).show();

将此行从

Toast.makeText(Temas.this, temas, Toast.LENGTH_LONG).show();


当您在匿名内部类
中时,此
将不会引用您当前的活动类

您应该使用
MainActivity.this
而不是
this

i、 e

你可以像这样重构你的代码,以摆脱开关的情况

String className= parent.getItemAtPosition(position).toString();
Class myClass=Class.forName("yourpackagename"+className);
Intent intent = new Intent(MainActivity.this, myClass);
startActivity(intent);
无需使用许多开关条件


同样正如上面的答案所指出的,使用
MainActivity。这是
Temas的安装。当您在匿名内部类
中时,这是您的toast消息中的
这将不涉及您当前的活动类

您应该使用
MainActivity.this
而不是
this

i、 e

你可以像这样重构你的代码,以摆脱开关的情况

String className= parent.getItemAtPosition(position).toString();
Class myClass=Class.forName("yourpackagename"+className);
Intent intent = new Intent(MainActivity.this, myClass);
startActivity(intent);
无需使用许多开关条件


同样正如上面的回答所指出的,使用
MainActivity。这是
Temas的安装。这是你的toast消息中的
,具体问题是什么?请注意,你是在Android Studio中编写代码的,它不是在启动活动,你的问题也不是关于Android Studio的。对不起,问题是我不知道使用ListView进行热启动活动。我尝试过使用该代码,但它标记了许多错误。具体问题是什么?请注意,您在Android Studio中编写代码,它不是启动活动,您的问题与Android Studio无关。抱歉,问题是我不知道使用ListView进行热启动活动。我尝试了代码,但它标记了很多错误。