2013-04-18 89 views
0

單擊列表視圖中的任何項目時我想要一個新的活動啓動。當我通過模擬器運行時,我的應用程序每次崩潰。我的積極態度是我的AndroidManifest也是正確的。任何幫助讚賞。Android應用程序在切換活動時崩潰

public static class SectionFragment extends ListFragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public static final String ARG_SECTION_NUMBER = "section_number"; 
    private String dataArrayOne[]; 
    private String dataArrayTwo[]; 

    public SectionFragment() { 
     dataArrayOne = new String[] { 
       "Steven's Portfolio", 
       "Sean's Portfolio", 
       "Logan's Portfolio", 
     }; 

     dataArrayTwo = new String[] { 
       "GOOG", 
       "YHOO", 
       "AAPL", 
       "MSFT" 
     }; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     switch(getArguments().getInt(ARG_SECTION_NUMBER)) 
     { 
     case 0: 
      ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, dataArrayOne); 
      setListAdapter(listAdapter); 
      break; 
     case 1: 
      ListAdapter listAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, dataArrayTwo); 
      setListAdapter(listAdapter1); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_main_dummy, container, false); 
    } 

    @Override 
    public void onListItemClick(ListView list, View v, int position, long id) 
    { 
     Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show(); 

     Intent intent = new Intent(getActivity(), Portfolio_Select.class); 
     intent.putExtra("USERNAME", getListView().getItemAtPosition(position).toString()); 
     startActivity(intent); 

    } 

AndroidManifest.xml中

<activity 
     android:name=".Portfolio_Select" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
</activity> 

Portfolio_Select代碼

public class Portfolio_Select extends Activity { 

    // variables go here 

    TextView display; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(com.example.stockticker.R.layout.activity_main); 



     display.setText("Stocks go here!"); 


    } 


} 
+1

發佈您正在收到的錯誤的堆棧跟蹤 - 它應該打印到logcat。 –

+1

顯示TextView爲空,所以你得到NullPointerException – Mike

回答

2
TextView display; 

聲明,但它總是引用null因爲findViewById()還沒有被用來給它任何東西來保存。

這意味着當

display.setText("Stocks go here!"); 

被調用時,你會得到一個NPE。

因此,使用確保你在R.layout.activity_mainTextView有一個ID,然後使用findViewById()displayTextView參考

setContentView(com.example.stockticker.R.layout.activity_main); 

display = (TextView) findViewById (R.id.your_text_view_id); 

display.setText("Stocks go here!"); 

如果有任何其他問題,堆棧跟蹤添加到您的問題,所以沒有其他猜測涉及。

相關問題