以下是我的类,当我运行应用程序时,它在选择时崩溃,并给我一个空错误。我正在设计一个修订应用程序,并试图做到这一点,而不必为涵盖的每个主题的类,任何帮助都会非常感谢。
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));
}
}发布于 2016-01-14 00:37:52
试着这样做
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");
}发布于 2016-01-14 00:39:00
你对你处理活动的方式的理解有一点错误。
通常,您永远不会自己实例化一个活动。
您需要研究如何使用Intents、startActivity和onActivityResult。
首先,创建一个TheoryTopicList的Intent,然后设置结果数据并完成该Activity。然后从onActivityResult读取选定的选项。
发布于 2016-01-14 01:25:46
在您的TheoryTopicList活动中,当您触发调用TheoryMain活动的意图时,您可以将数据与该意图一起发送。这些数据可以由TheoryMain类接收,并用于执行适当的操作。
在TheoryTopicList活动中向您的意图添加数据。
Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");
intent.putExtra("choice_selected", choice);//第一个参数是key,下一个参数是value//键,以便在下一个活动中检索该值。
startActivity(intent);从TheoryMain,中删除活动实例化行
ttl = new TheoryTopicList(); // Not needed and not good
ttl.getChoice(); // Not needed and not good现在检索上一个活动提供给您的数据。在你的onCreate of TheoryMain add中,
...
String choice = getIntent().getExtra("choice_selected"); // Using the key
switch(choice){
...
}https://stackoverflow.com/questions/34772026
复制相似问题