2016-01-13 182 views
1

中的另一個類中選定的列表項,這裏是我的類,當我運行它的應用程序崩潰的選擇,並給了我一個空錯誤。我正在設計一個修訂版應用程序,並且正在努力做到這一點,而不必爲涵蓋的每個主題創建一個類,任何幫助都將不勝感激。我怎樣才能獲得訪問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)); 
    } 
} 
+1

使用'Intent.putExtra'發送''意圖字符串''TheoryMain活動和''TheoryMain活動中使用'getIntent'接收選定的值。請參閱以下文章[將數據從一個活動轉移到另一個活動使用意圖](http://stackoverflow.com/questions/4967740/transfer-data-from-one-activity-to-another-activity-using-intents)可能有幫助 –

回答

0

嘗試做這樣的

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"); 

     } 
0

你的你活動的工作方式的理解是有點不對勁。

通常情況下,您從不親自實例活動。

您需要調查如何使用Intents,startActivityonActivityResult

首先,您創建一個IntentTheoryTopicList,然後設置結果數據並完成Activity。然後您從onActivityResult中讀取選定的選項。

0

在您的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){ 

... 
} 
0

替換此代碼:

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 =""; 
}