1. Check out our app, Forums for Android! Download and leave feedback here!

Problem launching new Activity

Discussion in 'Application Development' started by andywhoa, Jul 21, 2010.

  1. andywhoa

    andywhoa Well-Known Member
    Thread Starter
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    I have created a new Activity in my project
    Code (Text):
    1. public class ActivityInbox extends ListActivity implements OnClickListener, StateModelListener {
    I have added to to the AndroidManifest.xml:
    Code (Text):
    1.         <activity android:name=".ActivityInbox"></activity>
    I have added code to a button on my original screen to activate it
    Code (Text):
    1.             Intent intentActivityInbox = new Intent(ActivityLogin.this, ActivityInbox.class);
    2.             startActivity(intentActivityInbox);
    When the button is clicked the app explodes. I think it's down inside the following line of ActivityInbox
    Code (Text):
    1.         setContentView(R.layout.layout_inbox);

    Any ideas?
     

    Advertisement

  2. andywhoa

    andywhoa Well-Known Member
    Thread Starter
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    Here is my Activity code. Ignore the StateManager stuff. It's not hooked up
    Code (Text):
    1.  
    2. package com.mydomain.app.activity;
    3.  
    4. import java.util.ArrayList;
    5.  
    6. import android.app.ListActivity;
    7. import android.content.Context;
    8. import android.os.Bundle;
    9. import android.os.Handler;
    10. import android.view.LayoutInflater;
    11. import android.view.View;
    12. import android.view.ViewGroup;
    13. import android.view.View.OnClickListener;
    14. import android.widget.ArrayAdapter;
    15. import android.widget.TextView;
    16.  
    17. import com.mydomain.app.utils.*;
    18.  
    19. public class ActivityInbox extends ListActivity implements OnClickListener, StateModelListener {
    20.  
    21.     private ArrayList<MessageSummary> m_aryMessages = null;
    22.     private InboxAdapter m_Adapter;
    23.     private final Runnable m_runUpdateDisplay = new Runnable() {
    24.         public void run() {
    25.             updateDisplay();
    26.         }
    27.     };
    28.     private Runnable viewOrders;
    29.    
    30.     private static final String INBOX_STATE_MODEL = "InboxStateModel";
    31.    
    32.     // handler for thread posts
    33.     private final Handler m_hdlHandler = new Handler();
    34.    
    35.     // model to manage thread state
    36.     private StateModel m_smInbox;
    37.    
    38.     /** Called when the activity is first created. */
    39.     @Override
    40.     public void onCreate(Bundle savedInstanceState) {
    41.         super.onCreate(savedInstanceState);
    42.         setContentView(R.layout.layout_inbox);
    43.        
    44.         // if the first time in, create state model for activity
    45.         if (m_smInbox == null) {
    46.             m_smInbox = new StateModel();
    47.         }
    48.        
    49.         // TODO set pointers to ui controls
    50.  
    51.        
    52.         // TODO put cache logic here
    53.         if(savedInstanceState == null) {
    54.             viewOrders = new Runnable() {
    55.                 public void run() {
    56.                     launchInboxThread();
    57.                 }
    58.             };
    59.             // TODO update this to use State Model
    60.             Thread thread = new Thread(null, viewOrders, "MagentoBackground");
    61.             thread.start();
    62.         }
    63.     }
    64.    
    65.     @Override
    66.     protected void onPause() {
    67.         super.onPause();
    68.         // detach from the model
    69.         m_smInbox.setLoginModelListener(null);
    70.     }
    71.    
    72.     @Override
    73.     protected void onResume() {
    74.         super.onResume();
    75.         // attach to the model
    76.         m_smInbox.setLoginModelListener(this);
    77.        
    78.         // synchronize the display, in case the thread completed
    79.         // while this activity was not visible.  For example, if
    80.         // a phone call occurred while the thread was running.
    81.         updateDisplay();
    82.     }
    83.    
    84.     public void stateModelChanged(StateModel lm) {
    85.         // this may be called from a background thread, so post to the handler
    86.         m_hdlHandler.post(m_runUpdateDisplay);
    87.     }
    88.    
    89.     public void onClick(View v) {
    90.  
    91.     }
    92.    
    93.     @Override
    94.     protected void onSaveInstanceState(Bundle outState) {
    95.         super.onSaveInstanceState(outState);
    96.        
    97.         // save screen state
    98.         outState.putSerializable(INBOX_STATE_MODEL, m_smInbox);
    99.     }
    100.    
    101.     @Override
    102.     public void onRestoreInstanceState(Bundle savedInstanceState)
    103.     {
    104.         super.onRestoreInstanceState(savedInstanceState);
    105.        
    106.         // restore screen state
    107.         if (savedInstanceState != null) {
    108.             if (savedInstanceState.containsKey(INBOX_STATE_MODEL)) {
    109.                 m_smInbox = (StateModel)savedInstanceState.getSerializable(INBOX_STATE_MODEL);
    110.             }
    111.         }
    112.     }
    113.  
    114.     private final Runnable returnRes = new Runnable() {
    115.         public void run() {
    116.             if(m_aryMessages != null && m_aryMessages.size() > 0) {
    117.                 m_Adapter.notifyDataSetChanged();
    118.                 for(int i=0;i<m_aryMessages.size();i++)
    119.                     m_Adapter.add(m_aryMessages.get(i));
    120.                 m_Adapter.notifyDataSetChanged();
    121.             }
    122.         }
    123.     };
    124.    
    125.      
    126.     private void launchInboxThread() {
    127.         // TODO launch inbox thread
    128.         m_aryMessages = new ArrayList<MessageSummary>();
    129.         MessageSummary msgSum = new MessageSummary();
    130.         msgSum.setMessage("SF services");
    131.         msgSum.setSummary("Pending");
    132.         MessageSummary msgSum2 = new MessageSummary();
    133.         msgSum2.setMessage("SF advertisement");
    134.         msgSum2.setSummary("Completed");
    135.         m_aryMessages.add(msgSum);
    136.         m_aryMessages.add(msgSum2);
    137.        
    138.         runOnUiThread(returnRes);
    139.     }
    140.    
    141.     private void updateDisplay() {
    142.         // TODO update display
    143.     }
    144. }
    145.  
    146.  
    147.  
    148.  
    149.  
    150.  
    151.  
    152.  
    153.  
    154.  
    155. final class InboxAdapter extends ArrayAdapter<MessageSummary> {
    156.  
    157.     private ArrayList<MessageSummary> items;
    158.  
    159.     public InboxAdapter(Context context, int textViewResourceId, ArrayList<MessageSummary> items) {
    160.             super(context, textViewResourceId, items);
    161.             this.items = items;
    162.     }
    163.  
    164.     @Override
    165.     public View getView(int position, View convertView, ViewGroup parent) {
    166.             super.getView(position, convertView, parent);
    167.        
    168.             View v = convertView;
    169.             if (v == null) {
    170.                 LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    171.                 v = vi.inflate(R.layout.layout_inbox_item, null);
    172.             }
    173.             MessageSummary msgSum = items.get(position);
    174.             if (msgSum != null) {
    175.                     TextView tt = (TextView) v.findViewById(R.id.toptext);
    176.                     TextView bt = (TextView) v.findViewById(R.id.bottomtext);
    177.                     if (tt != null) {
    178.                           tt.setText("Name: "+msgSum.getMessage());
    179.                     }
    180.                     if(bt != null) {
    181.                           bt.setText("Status: "+ msgSum.getSummary());
    182.                     }
    183.             }
    184.             return v;
    185.     }
    186. }
    187.  


    Here is my layout xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7.     <ListView
    8.         android:id="@+id/android:list"
    9.         android:layout_width="fill_parent"
    10.         android:layout_height="fill_parent"
    11.     />
    12.     <TextView
    13.         android:id="@+id/android:empty"
    14.         android:layout_width="fill_parent"
    15.         android:layout_height="fill_parent"
    16.         android:text="@string/inbox_no_items"
    17.     />
    18. </LinearLayout>




    I have noticed that android:list and android:empty do not exist in R.java under the id tag. Is this ok? I'm following the tutorial here.
     
  3. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    does layout_inbox.xml contain a ListView object?

    Also, I THINK the listview must be contained in some kind of layout such as LinearLayout or something.
     
  4. andywhoa

    andywhoa Well-Known Member
    Thread Starter
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    Yes and yes. I've added the layout_inbox.xml to the previous post at the bottom
     
  5. andywhoa

    andywhoa Well-Known Member
    Thread Starter
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    Ah, my problem is I wasnt setting the adapter
     

Share This Page

Loading...