Wednesday, 13 April 2016

XML : Fragment give me Unfoutunately "APPNAME" has stopped

Ok Guys for some reason when i implement the fragment <> on the content_main.xml i get an error...

Here is my content_main.xml:

  <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:app="http://schemas.android.com/apk/res-auto"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      app:layout_behavior="@string/appbar_scrolling_view_behavior"      tools:context="com.notebook.notebook.MainActivity"      tools:showIn="@layout/activity_main">       <fragment         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:name="com.notebook.notebook.Fragment_2"         />    </LinearLayout>    

MainActivity.java:

  package com.notebook.notebook;    import android.os.Bundle;  import android.support.design.widget.FloatingActionButton;  import android.support.design.widget.Snackbar;  import android.support.v7.app.AppCompatActivity;  import android.support.v7.widget.Toolbar;  import android.view.View;  import android.view.Menu;  import android.view.MenuItem;  import android.widget.Toast;    public class MainActivity extends AppCompatActivity {        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);          setSupportActionBar(toolbar);      }        @Override      public boolean onCreateOptionsMenu(Menu menu) {          // Inflate the menu; this adds items to the action bar if it is present.          getMenuInflater().inflate(R.menu.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();            //noinspection SimplifiableIfStatement          if (id == R.id.action_settings) {              return true;          }            return super.onOptionsItemSelected(item);      }  }    

Fragment_2.java:

  package com.notebook.notebook;      import android.os.Bundle;  import android.support.annotation.Nullable;  import android.support.v4.app.Fragment;  import android.support.v4.app.ListFragment;  import android.view.LayoutInflater;  import android.view.View;  import android.view.ViewGroup;  import android.widget.ListView;    import com.notebook.notebook.R;    /**   * A simple {@link Fragment} subclass.   */  public class Fragment_2 extends ListFragment {        @Override      public void onListItemClick(ListView l, View v, int position, long id) {          super.onListItemClick(l, v, position, id);      }          @Override      public void onActivityCreated(@Nullable Bundle savedInstanceState) {          super.onActivityCreated(savedInstanceState);      }  }    

activity_main.xml:

  <?xml version="1.0" encoding="utf-8"?>  <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:app="http://schemas.android.com/apk/res-auto"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:fitsSystemWindows="true"      tools:context="com.notebook.notebook.MainActivity">        <android.support.design.widget.AppBarLayout          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:theme="@style/AppTheme.AppBarOverlay">            <android.support.v7.widget.Toolbar              android:id="@+id/toolbar"              android:layout_width="match_parent"              android:layout_height="?attr/actionBarSize"              android:background="?attr/colorPrimary"              app:popupTheme="@style/AppTheme.PopupOverlay" />        </android.support.design.widget.AppBarLayout>        <include layout="@layout/content_main" />        <android.support.design.widget.FloatingActionButton          android:id="@+id/fab"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="bottom|end"          android:layout_margin="@dimen/fab_margin"          android:src="@android:drawable/ic_dialog_email" />    </android.support.design.widget.CoordinatorLayout>    

A picture of my Android Studio:

enter image description here

And finnally i just wanted to say i am watching this viedo while im doing this. So this is where i got it from(Which is a 2016 Tutorial so very up to date):

https://www.youtube.com/watch?v=cjtWEDr3ynU&list=PLHs_NFdr_LaHmEh7hV-wPyS-gKnAVPzBU&index=39&nohtml5=False

Help Would Be Appreciated!

No comments:

Post a Comment