Telamnar Telamnar - 11 days ago 5
Java Question

Android - Trying to instantiate a class that is not a fragment

So I'm trying to create a fragment in my app that contains a toolbar (as an action bar) using appcompat activity so I can reuse it across multiple views.

I've tested a simple text-only fragment using a class that extends fragment only and got that to work, but to include a toolbar I need to extend AppCompatActivity in the view (I think).

When I try to get this to work, I get an error that says that my

action_bar_fragment
is not a
Fragment
.

I fell like I'm missing something basic here. Any ideas?

Main Activity:

package com.example.aaron.personaldataassistant;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.view.Menu;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {




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

// action_bar_fragment = (Toolbar) findViewById(R.id.action_bar_fragment);
// setSupportActionBar(action_bar_fragment);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater mi=getMenuInflater();
mi.inflate(R.menu.action_bar, menu);
return super.onCreateOptionsMenu(menu);
}


Main Activity Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"



android:id="@+id/activity_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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.aaron.personaldataassistant.MainActivity">


<fragment
android:id="@+id/action_bar_fragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.example.aaron.personaldataassistant.action_bar_fragment"
tools:layout="@layout/action_bar_fragment" />

</LinearLayout>


Action Bar Fragment:

package com.example.aaron.personaldataassistant;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


public class action_bar_fragment extends AppCompatActivity {

public action_bar_fragment() {
}

Toolbar action_bar;

@Nullable

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

return inflater.inflate(R.layout.action_bar_fragment, container, false);

}


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


action_bar = (Toolbar) findViewById(R.id.action_bar);
setSupportActionBar(action_bar);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.action_bar, menu);
return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

int i = item.getItemId();

if (i == R.id.item1) {

Toast.makeText(action_bar_fragment.this, "Item 1", Toast.LENGTH_SHORT).show();
} else if (i == R.id.item2) {

Toast.makeText(action_bar_fragment.this, "Item 2", Toast.LENGTH_SHORT).show();
} else if (i == R.id.item3) {

Toast.makeText(action_bar_fragment.this, "Item 3", Toast.LENGTH_SHORT).show();
}
//replace toasts with desired actions: Settings, Help and Exit. Settigns is blank, help is context-dependent, exit gives possibility to exit


return super.onOptionsItemSelected(item);
}


}


Fragment Layout:

<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="layout.Fragment_1">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />

<android.support.v7.widget.Toolbar
android:id="@+id/action_bar"
android:layout_width="300dp"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:elevation="8dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>

</FrameLayout>


And of course the error message:

--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aaron.personaldataassistant, PID: 2481
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aaron.personaldataassistant/com.example.aaron.personaldataassistant.MainActivity}: android.view.InflateException: Binary XML file line #19: Binary XML file line #19: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line #19: Binary XML file line #19: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.aaron.personaldataassistant.action_bar_fragment that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:617)
at android.app.Fragment.instantiate(Fragment.java:593)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2302)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:5884)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.aaron.personaldataassistant.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.ClassCastException
at android.app.Fragment.instantiate(Fragment.java:618)
at android.app.Fragment.instantiate(Fragment.java:593) 
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2302) 
at android.app.FragmentController.onCreateView(FragmentController.java:98) 
at android.app.Activity.onCreateView(Activity.java:5884) 
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36) 
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:426) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.example.aaron.personaldataassistant.MainActivity.onCreate(MainActivity.java:24) 
at android.app.Activity.performCreate(Activity.java:6662) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Application terminated.


Thank you for your time.

New Section

This is great, the top answer helped me figure out that doing things in a fragment can require different methods than doing them in an Activity. Googling "How do I do X in a fragment" turned out to give me almost everything I needed. There is only one problem left now: I changed the Action Bar Fragment line 37 to

setHasOptionsMenu(true);
action_bar = (Toolbar) getView().findViewById(R.id.action_bar);


This is giving me a null object reference, but the layout definitely exists! Any idea what's going on?

The relevant part of the error message is:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference


Sorry for the long delay getting back to you, I had work. Should be faster this time!
I will also clean up the comment I made on your reply.

Answer

This is because your intended fragment is not a Fragment but an Activity, where:

action_bar_fragment extends AppCompatActivity {

}

is an Activity, so you should be extends action_bar_fragment with Fragment:

action_bar_fragment extends Fragment {

}

And please change your code style, where action_bar_fragment should be something like ActionBarFragment. A class name should be started with uppercase.

Please read Creating and Using Fragments.

Comments