Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Android App Crashes On Startup

  • 06-09-2014 7:47pm
    #1
    Closed Accounts Posts: 1,312 ✭✭✭


    Hi everyone,

    I've just started teaching myself Android development, and I've already hit a bit of a hurdle. I'm writing a simple shopping list app (following a tutorial really). Basically, I can make the app display the interface no problem, but as soon as I try to actually make it do anything in the activity code, the app simply stops launching.

    Here's the Java, including everything generated by Eclipse:
    (The problem is caused by the .setOnClickListener and .setChecked calls. When I comment those out, I can launch the app.)
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class ShoppingListActivity extends ActionBarActivity implements
            OnClickListener {
    
        EditText et1 = null;
        CheckBox cb1 = null;
        EditText et2 = null;
        CheckBox cb2 = null;
        EditText et3 = null;
        CheckBox cb3 = null;
        EditText et4 = null;
        CheckBox cb4 = null;
        EditText et5 = null;
        CheckBox cb5 = null;
        TextView tv = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_shopping_list);
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment()).commit();
            }
            
            et1 = (EditText) findViewById(R.id.item1);
            cb1 = (CheckBox) findViewById(R.id.check1);
            et2 = (EditText) findViewById(R.id.item2);
            cb2 = (CheckBox) findViewById(R.id.check2);
            et3 = (EditText) findViewById(R.id.item3);
            cb3 = (CheckBox) findViewById(R.id.check3);
            et4 = (EditText) findViewById(R.id.item4);
            cb4 = (CheckBox) findViewById(R.id.check4);
            et5 = (EditText) findViewById(R.id.item5);
            cb5 = (CheckBox) findViewById(R.id.check5);
            tv = (TextView) findViewById(R.id.done);
            cb1.setOnClickListener(this);
            cb2.setOnClickListener(this);
            cb3.setOnClickListener(this);
            cb4.setOnClickListener(this);
            cb5.setOnClickListener(this);
            cb1.setChecked(false);
            cb2.setChecked(false);
            cb3.setChecked(false);
            cb4.setChecked(false);
            cb5.setChecked(false);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
    
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.shopping_list, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
    
        }
    
        /**
         * A placeholder fragment containing a simple view.
         */
        public static class PlaceholderFragment extends Fragment {
    
            public PlaceholderFragment() {
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_shopping_list,
                        container, false);
                return rootView;
            }
        }
    
    }
    
    And the XML:
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.shoppinglist.MainActivity$PlaceholderFragment" >
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <EditText
                android:id="@+id/item1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="250dp"
                android:paddingBottom="5dp" />
    
            <CheckBox android:id="@+id/check1" />
        </TableRow>
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <EditText
                android:id="@+id/item2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="250dp"
                android:paddingBottom="5dp" />
    
            <CheckBox android:id="@+id/check2" />
        </TableRow>
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <EditText
                android:id="@+id/item3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="250dp"
                android:paddingBottom="5dp" />
    
            <CheckBox android:id="@+id/check3" />
        </TableRow>
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <EditText
                android:id="@+id/item4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="250dp"
                android:paddingBottom="5dp" />
    
            <CheckBox android:id="@+id/check4" />
        </TableRow>
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <EditText
                android:id="@+id/item5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="250dp"
                android:paddingBottom="5dp" />
    
            <CheckBox android:id="@+id/check5" />
        </TableRow>
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <DigitalClock
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20pt" />
        </TableRow>
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <TextView
                android:id="@+id/done"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                android:minWidth="250dp"
                android:paddingBottom="5dp"
                android:text=""
                android:textColor="#ff0000"
                android:textSize="20pt" >
            </TextView>
        </TableRow>
    </TableLayout>
    
    The logcat output isn't helping me very much, but I think this is the relevant part:
    09-06 15:25:30.611: E/AndroidRuntime(3624): Caused by: java.lang.NullPointerException
    09-06 15:25:30.611: E/AndroidRuntime(3624):     at com.example.shoppinglist.ShoppingListActivity.onCreate(ShoppingListActivity.java:51)
    09-06 15:25:30.611: E/AndroidRuntime(3624):     at android.app.Activity.performCreate(Activity.java:5231)
    09-06 15:25:30.611: E/AndroidRuntime(3624):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    09-06 15:25:30.611: E/AndroidRuntime(3624):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    
    If anyone can put me out of my misery here, I'd really appreciate it. I've been banging my head off this one for hours.


Comments

  • Registered Users, Registered Users 2 Posts: 1,148 ✭✭✭punk_one82


    I'm not an Android developer, but try removing the initialisation of your EditText and Checkbox variables to null. Doesn't seem necessary and I don't think it's correct.


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    Setting them to null is a bit pointless, but it's not incorrect. I don't do Android either, but those findViewById calls must be failing. But which one? You don't do error checking?


  • Closed Accounts Posts: 1,312 ✭✭✭Daftendirekt


    Thanks for the replies, I've got it sussed now.

    In case anyone's wondering, I had my XML in a file called fragment_shopping_list rather than activity_shopping_list. Silly mistake really.

    Rather than start a new thread, I might as well ask here - are there any really good Android development books that people would recommend? The one I'm working through at the moment is quite dated. (Makes no mention of fragments, for instance, and from what I've been reading, they're quite important.)


  • Registered Users, Registered Users 2 Posts: 296 ✭✭conti


    This is a great book, I started with this mixed with some video tutorials and i was writing apps in no time. I started learning Android in February and now I'm working in app development, although I did have Java beforehand. It has a chapter on fragments that makes them very easy to understand IMO.

    http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/ref=la_B001ITVV1E_1_1?s=books&ie=UTF8&qid=1410079012&sr=1-1

    This is another one I went through, not as up to date covers slightly more ground with more detailed explanations. Both books are good to have as reference.

    http://www.amazon.com/Professional-Android-4-Application-Development/dp/1118102274


  • Closed Accounts Posts: 816 ✭✭✭Opinicus


    I'd recommend CommonsWare for a straightforward and concise guide. The author often posts on StackOverflow too.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 18,272 ✭✭✭✭Atomic Pineapple


    Highly recommend the Android fundamentals course on Udacity - https://www.udacity.com/course/ud853


  • Closed Accounts Posts: 1,312 ✭✭✭Daftendirekt


    Cheers for the recommendations, that gives me plenty to work with!


Advertisement