0

I've been having this issue, this is my first android app and I probably did something stupid. I am a total noob at this and this has been driving me insane. Running the app will give me the error, "Unfortunately -app- has stopped working." Help would be appreciated

Fragment Main:

<RelativeLayout 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: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.gmail.sherwoodax.appone.MainActivity$PlaceholderFragment" >

    <Button
        android:id="@+id/dBID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dead Button" 
        android:layout_centerInParent="true"

        />

    <TextView
        android:id="@+id/tView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="The Button is below me"
        android:layout_above="@+id/dBID"
        android:gravity="center"
        android:textSize="20sp"

        />

</RelativeLayout>

Main Class:

package com.gmail.sherwoodax.appone;



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.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

            testMethod();
        }



    }


    private void testMethod() {

        Button b = (Button) findViewById(R.id.dBID);

        b.setOnClickListener(new View.OnClickListener() {

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

                Toast.makeText(MainActivity.this, "text", Toast.LENGTH_LONG).show();

            }
        });

    }


    @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;
        }
    }

}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gmail.sherwoodax.appone"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gmail.sherwoodax.appone.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

LogCat stacktrace:

05-28 11:15:54.977: E/AndroidRuntime(31586): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.gmail.sherwoodax.appone:id/container) for fragment PlaceholderFragment{41c157b0 #0 id=0x7f05003c}
05-28 11:20:47.344: E/AndroidRuntime(32409): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.gmail.sherwoodax.appone:id/container) for fragment PlaceholderFragment{41c10d00 #0 id=0x7f05003c}

Thank you

  • 1
    That error message on the device screen is for end users, and provides no real information to you, the developer. Learn to use the logcat tool to find the stack trace of the actual failure. – Chris Stratton May 27 '14 at 22:20
  • I did look through the logcat tool, but it didn't really help me, mostly because I don't understand it too well – user3681585 May 27 '14 at 22:22
  • 1
    @user3681585 The LogCat output usually displays multiple stack traces. The first one won't be helpful, but if you look down you should see a line that says `Caused by:`, and the first lines at that point usually (but not always) tell you what line of your program caused the problem, and what exception was thrown. Sometimes you have to look further for another `Caused by:`. – ajb May 27 '14 at 22:24
  • @ajb No view found for ID, doesn't it automatically create the ID when I do @+id/dBID ? – user3681585 May 27 '14 at 22:26
  • If it didn't create the ID, your program would fail to compile because it wouldn't find `R.id.whatever`. So it's something else. Please copy the `Caused by:` line and probably the next few lines of the LogCat, edit your question and paste in the LogCat lines, and show us in your source code what line it refers to (since we can't always tell). Hopefully that will help someone help you figure it out--I don't think I can. – ajb May 27 '14 at 22:32
  • 1
    I'm not an expert, but you probably shouldn't have `@+id/dBID` twice in your layout. Probably you should remove the `+` from the second one? But I could be wrong... – ajb May 27 '14 at 22:34
  • @ajb Yes, i believe that was another error I made, i changed that, and although it's still giving me the same error, the logcat is only giving me a NullPointerException, before it was a billion lines of stuff. – user3681585 May 27 '14 at 23:07
  • Is the XML file you supplied the layout that is being set as the contentView in your activity (i.e. is it activity_main) or is it the fragment layout? – Rarw May 27 '14 at 23:18
  • 1
    Post the stack trace. Otherwise we're just guessing what might be wrong. – Brodo Fraggins May 27 '14 at 23:45
  • The *whole* stack trace, including all the "at" lines. – Brodo Fraggins May 29 '14 at 21:37

1 Answers1

0

Please post the logcat stacktrace for the crash so we can help pinpoint the exact problem. However, I would guess that it has to do with your Button.

In your testMethod() you declare:

Button b = (Button) findViewById(R.id.dBID);

But because it is declared inside this method it is not within scope for your activity and exists only inside that single method call and is lost afterwards.

You should declare your button as a class level variable in your Activity/Fragment:

public class MainActivity extends ActionBarActivity {
    private Button myButton;
    ...

then assign the Button in your onCreate() (or onViewCreate() for a Fragment):

public void onCreate(...) {
    myButton = (Button) findViewById(...);
    ...

and then anywhere else in your class you want to use that Button you can refer to it without needing to pull and cast it again from your view:

button.setText(newButtonText);
indivisible
  • 4,892
  • 4
  • 31
  • 50