我怎样才能获得访问Android
问题描述:
中的另一个类中选定的列表项,这里是我的类,当我运行它的应用程序崩溃的选择,并给了我一个空错误。我正在设计一个修订版应用程序,并且正在努力做到这一点,而不必为涵盖的每个主题创建一个类,任何帮助都将不胜感激。我怎样才能获得访问Android
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));
}
}
答
尝试做这样的
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");
}
答
你的你活动的工作方式的理解是有点不对劲。
通常情况下,您从不亲自实例活动。
您需要调查如何使用Intents,startActivity和onActivityResult。
首先,您创建一个Intent
到TheoryTopicList
,然后设置结果数据并完成Activity
。然后您从onActivityResult
中读取选定的选项。
答
在您的TheoryTopicList活动中,当您启动旨在调用TheoryMain活动的Intent时,可以发送数据以及该意图。这些数据可以由TheoryMain类接收并用于执行适当的操作。
在TheoryTopicList活动中向您的意图添加数据。
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
现在找回以前的活动给你的数据。在你TheoryMain中的onCreate增加,
...
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);
并在TheoryMain活动对创建方法。
替换此
ttl = new TheoryTopicList();
ttl.getChoice();
与
if(getIntent().hasExtra("choice"))
{
choice = getIntent().getStringExtra("choice");
}
else{
choice ="";
}
使用'Intent.putExtra'发送''意图字符串''TheoryMain活动和''TheoryMain活动中使用'getIntent'接收选定的值。请参阅以下文章[将数据从一个活动转移到另一个活动使用意图](http://*.com/questions/4967740/transfer-data-from-one-activity-to-another-activity-using-intents)可能有帮助 –