Java 如何在android的另一个类中访问选定的列表项

Java 如何在android的另一个类中访问选定的列表项,java,android,listactivity,Java,Android,Listactivity,下面是我的类,当我运行应用程序时,它会在选择时崩溃,并给我一个空错误。我正在设计一个修订版应用程序,并试图做到这一点,而不必为所涉及的每一个主题都上一节课。任何帮助都将不胜感激 import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import org.w3c.dom.Text; public class TheoryMain extends Activity {

下面是我的类,当我运行应用程序时,它会在选择时崩溃,并给我一个空错误。我正在设计一个修订版应用程序,并试图做到这一点,而不必为所涉及的每一个主题都上一节课。任何帮助都将不胜感激

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

public class TheoryMain extends Activity {
    TheoryTopicList ttl;
    TextView tv1;
    String choice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.theory_layout);
        ttl = new TheoryTopicList();
        ttl.getChoice();
        tv1 = (TextView) findViewById(R.id.theory_tv);
        switch(choice){
            case("Photo-Electric effect"):
                tv1.setText(getString(R.string.photo_electric));
                break;
            case("Photons and Electrons"):
                tv1.setText(getString(R.string.photons_electrons));
                break;
            case("de Broglie wavelength"):
                tv1.setText(getString(R.string.de_broglie));
                break;
            case("Types of particles"):
                tv1.setText(getString(R.string.particles));
                break;
            case("Interactions"):
                tv1.setText(getString(R.string.interactions));
                break;
            case("Radiation"):
                tv1.setText(getString(R.string.radiation));
                break;
            case("Voltage, Current and Resistance(Ohms law)"):
                tv1.setText(getString(R.string.ohms_law));
                break;
            case("Circuits"):
                tv1.setText(getString(R.string.circuits));
                break;
            case("Power and Efficiency"):
                tv1.setText(getString(R.string.power_efficiency));
                break;
            case("Alternating Current and oscilloscope graphs"):
                tv1.setText(getString(R.string.ac_graphs));
                break;
            case("E.M.F and internal Resistance"):
                tv1.setText(getString(R.string.emf_resistance));
                break;
        }
    }
}






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

public class TheoryTopicList extends ListActivity {
    String[] display = {"Photo-Electric effect", "Photons and Electrons", "de Broglie wavelength", "Types of particles",
                        "Interactions", "Radiation", "Voltage, Current and Resistance(Ohms law)", "Circuits",
                        "Power and Efficiency","Alternating Current and oscilloscope graphs", "E.M.F and internal Resistance"};
    String choice;
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);
        choice = display[position];
        Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");
        startActivity(intent);
    }

    public String getChoice(){
        return choice;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(TheoryTopicList.this,android.R.layout.simple_list_item_1,display));
    }
}
导入android.app.Activity;
导入android.os.Bundle;
导入android.widget.TextView;
导入org.w3c.dom.Text;
公共课理论扩展活动{
理论药典;
文本视图tv1;
字符串选择;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.theory_layout);
ttl=新的TheoreToPicList();
ttl.getChoice();
tv1=(TextView)findViewById(R.id.theory\U tv);
开关(选择){
案例(“光电效应”):
tv1.setText(getString(R.string.photo_electric));
打破
案例(“光子和电子”):
tv1.setText(getString(R.string.photons_-electrons));
打破
案例(“德布罗意波长”):
tv1.setText(getString(R.string.de_broglie));
打破
案例(“粒子类型”):
setText(getString(R.string.particles));
打破
案例(“互动”):
setText(getString(R.string.interactions));
打破
案例(“辐射”):
tv1.setText(getString(R.string.radiation));
打破
外壳(“电压、电流和电阻(欧姆定律)”):
tv1.setText(getString(R.string.ohms_定律));
打破
案例(“电路”):
tv1.setText(getString(R.string.circuits));
打破
案例(“功率和效率”):
tv1.setText(getString(R.string.power_-efficiency));
打破
案例(“交流和示波器图表”):
tv1.setText(getString(R.string.ac_图));
打破
案例(“电动势和内阻”):
tv1.setText(getString(R.string.emf_resistance));
打破
}
}
}
导入android.app.ListActivity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
公共类TheoryTopicList扩展了ListActivity{
String[]display={“光电效应”、“光子和电子”、“德布罗意波长”、“粒子类型”,
“相互作用”、“辐射”、“电压、电流和电阻(欧姆定律)”、“电路”,
“功率和效率”、“交流电和示波器图”、“电动势和内阻”};
字符串选择;
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
选择=显示[位置];
意向意向=新意向(“com.revisonapp.alec.alevelphysics.THEORYMAIN”);
星触觉(意向);
}
公共字符串getChoice(){
回报选择;
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(新的ArrayAdapter(TheoryTopicList.this,android.R.layout.simple_list_item_1,display));
}
}
试着这样做

Intent intent=new Intent(this,AnotherClass.class);
intent.putExtra("yourStringVal",yourStringVal);
startActivity(intent);
and in your another class try 2 get these items by doing

  Bundle extras = getIntent().getExtras();
        if(extras!=null){
            String yourStringVal=extras.getString("yourStringVal");

        }
试着这样做

Intent intent=new Intent(this,AnotherClass.class);
intent.putExtra("yourStringVal",yourStringVal);
startActivity(intent);
and in your another class try 2 get these items by doing

  Bundle extras = getIntent().getExtras();
        if(extras!=null){
            String yourStringVal=extras.getString("yourStringVal");

        }

你对活动方式的理解有点错误

通常情况下,您自己从不引用某项活动

您需要调查如何使用,以及


首先,您创建一个
意图
理论主题列表
,然后设置结果数据并完成该
活动
。然后,您从ActivityResult的
中读取所选选项

您对处理活动方式的理解有点错误

通常情况下,您自己从不引用某项活动

您需要调查如何使用,以及


首先,您创建一个
意图
理论主题列表
,然后设置结果数据并完成该
活动
。然后,在TheoryTopicList活动中,从ActivityResult的
中读取所选选项。当您触发调用TheoryMain活动的意图时,您可以随该意图一起发送数据。TheoryMain类可以接收这些数据并用于执行适当的操作

在TheoreToPicList活动中向您的意图添加数据。

Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");
intent.putExtra("choice_selected", choice);
//第一个参数是键,下一个是值。 //键以在下一个活动中检索值

startActivity(intent);
从TheoryMain中删除活动实例化行,

ttl = new TheoryTopicList(); // Not needed and not good
ttl.getChoice(); // Not needed and not good
...

String choice = getIntent().getExtra("choice_selected"); // Using the key
switch(choice){

...
}
现在检索上一个活动提供给您的数据。在您的onCreate of Theory中添加,

ttl = new TheoryTopicList(); // Not needed and not good
ttl.getChoice(); // Not needed and not good
...

String choice = getIntent().getExtra("choice_selected"); // Using the key
switch(choice){

...
}

在TheoryTopicList活动中,当您激发调用TheoryMain活动的意图时,可以随该意图一起发送数据。TheoryMain类可以接收这些数据并用于执行适当的操作

在TheoreToPicList活动中向您的意图添加数据。

Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");
intent.putExtra("choice_selected", choice);
//第一个参数是键,下一个是值。 //键以在下一个活动中检索值

startActivity(intent);
从TheoryMain中删除活动实例化行,

ttl = new TheoryTopicList(); // Not needed and not good
ttl.getChoice(); // Not needed and not good
...

String choice = getIntent().getExtra("choice_selected"); // Using the key
switch(choice){

...
}
现在检索上一个活动提供给您的数据。在您的onCreate of Theory中添加,

ttl = new TheoryTopicList(); // Not needed and not good
ttl.getChoice(); // Not needed and not good
...

String choice = getIntent().getExtra("choice_selected"); // Using the key
switch(choice){

...
}

替换此代码:

Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");

startActivity(intent);

Intent intent = new Intent(TheoryTopicList .this,TheoryMain .class);

intent.putExtra("choice",choice);

startActivity(intent);
ttl = new TheoryTopicList();
ttl.getChoice();
以及理论上主要活动的创建方法。

Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");
intent.putExtra("choice_selected", choice);
替换此

Intent intent = new Intent(TheoryTopicList .this,TheoryMain .class);

intent.putExtra("choice",choice);

startActivity(intent);
ttl = new TheoryTopicList();
ttl.getChoice();


替换此代码:

Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");

startActivity(intent);

Intent intent = new Intent(TheoryTopicList .this,TheoryMain .class);

intent.putExtra("choice",choice);

startActivity(intent);
ttl = new TheoryTopicList();
ttl.getChoice();