-2

Edit: I changed my content view to setContentView(R.layout.fragment_main); the application still crashes. I do not quite understand the reason for this.

This is a simple application for detecting and counting the number of potholes felt on a road. I have read many threads on this issue and implemented many different changes to my code. But none seem to work out in fixing the error that I have. Where exactly am I going wrong in my code here?

MainActivity.java

package com.example.potholedetector;

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.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements Button.OnClickListener {


    // Private member field to keep track of the number of potholes
    private static int num_potholes = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        //Restore any saved state
        super.onCreate(savedInstanceState);

        //Set content view
        setContentView(R.layout.activity_main);

        //Initialize UI elements 
        final Button pothole = (Button)findViewById(R.id.pothole);
        final TextView NumPotholes = (TextView)findViewById(R.id.NumPotholes);

        //Link UI elements to actions in code
            pothole.setOnClickListener(new Button.OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                    num_potholes++;
                    NumPotholes.setText("Number of potholes felt: " + num_potholes);
                }
            });

        if (savedInstanceState == null) 
        {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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);
    }

    /**
     * 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_main, container, false);
            return rootView;

        /** Called when the user clicks the Pothole button */

        }      
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }


}

fragment_main.xml

<LinearLayout 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.potholedetector.MainActivity$PlaceholderFragment" >

    <TextView android:id="@+id/pothole_message"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="@string/pothole_message"
        android:textSize="20sp"/>
    <Button android:id="@+id/pothole"
        android:layout_width="wrap_content"
        android:minWidth="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/pothole"
        android:onClick="potholeFelt"/>
    <TextView android:id="@+id/NumPotholes"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textSize="20sp"/>
</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">PotholeDetector</string>
    <string name="pothole_message">Pothole felt?</string>
    <string name="pothole">Pothole</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>

I tried to carefully follow many tutorials, but after spending so much time debugging, I am no closer to eradicating the error and moving on. How can I make this code work?

user3694391
  • 133
  • 1
  • 2
  • 12
  • go to this [http://stackoverflow.com/questions/23869019/nullpointerexception-thrown-when-trying-to-findviewbyid/23869046#23869046](http://stackoverflow.com/questions/23869019/nullpointerexception-thrown-when-trying-to-findviewbyid/23869046#23869046) – M D May 31 '14 at 14:39
  • you should move your all Button reference code under your Fragment. take a look in my above given link. – M D May 31 '14 at 14:54
  • I looked into that before. It leaves me a bit confused because according to some tutorials, this java code is supposed to be in MainActivity.java, not in fragment.xml. When I place the pothole.setOnClickListener code (the block under "Link UI elements to action in code") in fragment.xml, I get a warning saying "Unexpected text found in layout file". Is that what you meant to say? – user3694391 May 31 '14 at 15:12

1 Answers1

1

Your app is crashing for NPE, since you are passing as parameter R.layout.activity_main to setContentView, but the elements you are looking for are declared inside fragment_main.xml. Call setContentView(R.layout.fragment_main);

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 1
    I changed it to setContentView(R.layout.fragment_main); but that doesn't seem to solve the issue. The application is still crashing. Am I doing anything else wrong? – user3694391 May 31 '14 at 14:42