I am slightly new to Android studio and have the following code for a settings page using a fragment. I used a settingsFragment and settingsActivity to create the settings page. I also created a button in the mainActivity that should take me to the settings page. But whenever I run it, the app just closes. Im not sure how to fix this. Could somebody please take a look?
MainActivity.java:
package com.example.settingspagecs309;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openSettingsActivity();
}
});
}
public void openSettingsActivity(){
Intent intent=new Intent(this,SettingsActivity.class);
startActivity(intent);
}
}
ActivityMain.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open settings"/>
</RelativeLayout>
SettingsFragment.java:
package com.example.settingspagecs309;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.Preference;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences,rootKey);
Preference myPref= (Preference) findPreference("Contact_Preference");
myPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
startActivity(new Intent(getContext(),MainActivity.class));
return true;
}
});
}
}
SettingsActivity.java:
package com.example.settingspagecs309;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction().replace(android.R.id.content,new SettingsFragment()).commit();
ActionBar menu=getSupportActionBar();
menu.setDisplayShowHomeEnabled(true);
menu.setDisplayShowHomeEnabled(true);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}
This is the error message it is showing:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.settingspagecs309, PID: 29462
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.settingspagecs309/com.example.settingspagecs309.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.preference.Preference.setOnPreferenceClickListener(androidx.preference.Preference$OnPreferenceClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.preference.Preference.setOnPreferenceClickListener(androidx.preference.Preference$OnPreferenceClickListener)' on a null object reference
at com.example.settingspagecs309.SettingsFragment.onCreatePreferences(SettingsFragment.java:22)
at androidx.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:228)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1277)
at android.app.Activity.performStart(Activity.java:6306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
D/AppTracker: App Event: crash
I/Process: Sending signal. PID: 29462 SIG: 9
Related
I constantly have this message when I launch my app !!!SORRY YOUR APP unfortunately stopped or can't launch. I don't really know how it came about. Can some help me out with this error please. Equally I don't get any error message after launching only when app has to launch.
here is the logcat error message:
and its layout activity
my main activity code
01-02 12:52:57.694 5065-5065/com.logo.tm.logodrive001 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.logo.tm.logodrive001, PID: 5065
java.lang.NoSuchMethodError: No static method getFont(Landroid/content/Context;ILandroid/util/TypedValue;ILandroid/widget/TextView;)Landroid/graphics/Typeface; in class Landroid/support/v4/content/res/ResourcesCompat; or its super classes (declaration of 'android.support.v4.content.res.ResourcesCompat' appears in /data/app/com.logo.tm.logodrive001-1/split_lib_dependencies_apk.apk)
at android.support.v7.widget.TintTypedArray.getFont(TintTypedArray.java:119)
at android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:208)
at android.support.v7.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:110)
at android.support.v7.widget.AppCompatTextHelperV17.loadFromAttributes(AppCompatTextHelperV17.java:38)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:81)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:71)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:67)
at android.support.v7.widget.Toolbar.setTitle(Toolbar.java:753)
at android.support.v7.widget.ToolbarWidgetWrapper.setTitleInt(ToolbarWidgetWrapper.java:261)
at android.support.v7.widget.ToolbarWidgetWrapper.setWindowTitle(ToolbarWidgetWrapper.java:243)
at android.support.v7.widget.ActionBarOverlayLayout.setWindowTitle(ActionBarOverlayLayout.java:621)
at android.support.v7.app.AppCompatDelegateImplV9.onTitleChanged(AppCompatDelegateImplV9.java:631)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.logo.tm.logodrive001.MainActivity.onCreate(MainActivity.java:14)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button mDriver, mCustomer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDriver = (Button) findViewById(R.id.driver);
mCustomer = (Button) findViewById(R.id.customer);
startService(new Intent(MainActivity.this, onAppKilled.class));
mDriver.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DriverLoginActivity.class);
startActivity(intent);
finish();
return;
}
});
mCustomer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CustomerLoginActivity.class);
startActivity(intent);
finish();
return;
}
});
}
}
<?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"
android:background="#drawable/background"
tools:context="com.logo.tm.logodrive001.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="log in 1"
android:id="#+id/driver"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="log in 2"
android:id="#+id/customer"/>
</LinearLayout>
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
/**
* Created by name on 3/13/17.
* Used for the specials tab to allow a list of days to choose what special one would like to view
*/
public class SpecialsScroll extends ListFragment implements AdapterView.OnItemClickListener{
public SpecialsScroll(){
//Empty Constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.fragment_specials_scroll, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getActivity(), R.array.days, android.R.layout.simple_list_item_1)); //Loads array into the ListFragment
getListView().setOnItemClickListener(this); //activates the listener for this Fragment class
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){ // For checking when a user taps on an option
SpecialsPage specialsPage = new SpecialsPage();
Bundle foodArgs = new Bundle();
switch (position){ // checks what option is chosen and sends respective keys as parameters
case 0:
int[] keysMonday = {R.string.boom_boom_enchiladas_key, R.string.tacos_mexican_key};
foodArgs.putIntArray("keys", keysMonday);
break;
case 1:
int[] keysTuesday = {R.string.shrimp_avocado_tacos_key, R.string.green_chile_chicken_enchiladas_key};
foodArgs.putIntArray("keys", keysTuesday);
break;
}
specialsPage.setArguments(foodArgs);
getActivity().getFragmentManager().beginTransaction().replace(R.id.container, specialsPage).addToBackStack(null).commit();
}
}
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity{
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if(getFragmentManager().findFragmentByTag("CURRENT") == null){
getFragmentManager().beginTransaction().add(R.id.container, new SpecialsScroll(), "CURRENT").commit();
}
switch (item.getItemId()) {
case R.id.special:
getFragmentManager().beginTransaction().replace(R.id.container, new SpecialsScroll(), "CURRENT").commit();
return true;
case R.id.locations:
getFragmentManager().beginTransaction().replace(R.id.container, new LocationsScroll(), "CURRENT").commit();
return true;
case R.id.order:
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
getFragmentManager().beginTransaction().add(R.id.container, new SpecialsScroll(), "CURRENT").commit();
}
}
So, I am trying to create a ListView and everything in the code works, but suddenly I am unable to get the xml to recognize the android:id/list attribute for some reason, I don't know why. When I click on an item in the listview in my app it still does what it should, I just really want to get rid of the error.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<TextView
android:id="#+id/text_specialsscroll"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
04-26 14:06:30.299 2576-2576
/com.namename.www.name
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.namename.www.name, PID: 2576
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.namename.www.name/com.namename.www.name.MainActivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
at android.app.ListFragment.ensureList(ListFragment.java:402)
at android.app.ListFragment.onViewCreated(ListFragment.java:203)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1010)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
at android.app.BackStackRecord.run(BackStackRecord.java:816)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
at android.app.FragmentController.execPendingActions(FragmentController.java:371)
at android.app.Activity.performStart(Activity.java:6695)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Change your id to this:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
below find the java file i dont no where it gone wrong but my app is unfortunately stopping ple can some one help me where and in which line i am going wrong.....
below is my logcat ... details
FATAL EXCEPTION: main
Process: com.example.ankita.implicitintent, PID: 16822
java.lang.NullPointerException
at com.example.ankita.implicitintent.MainActivity.onClick(MainActivity.java:82)
at android.view.View.performClick(View.java:4651)
at android.view.View$PerformClick.run(View.java:19310)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
06-27 17:17:40.025 16822-16822/com.example.ankita.implicitintent I/Process: Sending signal. PID: 16822 SIG: 9
package com.example.ankita.implicitintent;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button imgssend;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgssend=(Button)findViewById(R.id.images_id);
imgssend.setOnClickListener(this);
}
#Override
public void onClick(View view)
{
Intent i=null,chooser=null;
switch (view.getId())
{
case R.id.images_id:
File Pictures= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String[] listofPictures=Pictures.list();
Uri uri=null;
ArrayList<Uri> arraylist=new ArrayList<Uri>();
for(String picture:listofPictures)
{
uri=uri.parse("file://"+Pictures.toString()+"/"+picture);
arraylist.add(uri);
}
i=new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM,arraylist);
chooser=Intent.createChooser(i,"Send multiple");
startActivity(chooser);
break;
}
}
}
here is xml file
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.ankita.implicitintent.MainActivity">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/send_images"
android:id="#+id/images_id"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Looks Like Something is wrong with the OnClick Override. Why dont you use the onClick Listener of the Button?
Something like this:
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button imgssend=(Button)findViewById(R.id.images_id);
imgssend.setOnClickListener(new OnClickListener{
#Override
public void onClick(View v) {
Intent i=null,chooser=null;
File Pictures= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String[] listofPictures=Pictures.list();
Uri uri=null;
ArrayList<Uri> arraylist=new ArrayList<Uri>();
for(String picture:listofPictures)
{
uri=uri.parse("file://"+Pictures.toString()+"/"+picture);
arraylist.add(uri);
}
i=new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM,arraylist);
chooser=Intent.createChooser(i,"Send multiple");
startActivity(chooser);
}
});
}
}
I'm getting the below error when trying out the Fragments in Android Studio. The below exception occurs when launching the app :
04 - 24 21: 02: 16.830 21108 - 21108 / com.example.user.samplefragments E / AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo {
com.example.user.samplefragments / com.example.user.samplefragments.MainActivity
}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line # 7: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java: 752)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 495)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 397)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java: 364)
at android.app.Activity.setContentView(Activity.java: 1930)
at com.example.user.samplefragments.MainActivity.onCreate(MainActivity.java: 19)
at android.app.Activity.performCreate(Activity.java: 5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2299)
at android.app.ActivityThread.access$700(ActivityThread.java: 150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1280)
at android.os.Handler.dispatchMessage(Handler.java: 99)
at android.os.Looper.loop(Looper.java: 137)
at android.app.ActivityThread.main(ActivityThread.java: 5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.user.samplefragments.MenuFragment.onCreateView(MenuFragment.java: 23)
at android.app.Fragment.performCreateView(Fragment.java: 1699)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java: 879)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java: 1053)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java: 1155)
at android.app.Activity.onCreateView(Activity.java: 4872)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 686)
at android.view.LayoutInflater.rInflate(LayoutInflater.java: 752)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 495)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 397)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java: 364)
at android.app.Activity.setContentView(Activity.java: 1930)
at com.example.user.samplefragments.MainActivity.onCreate(MainActivity.java: 19)
at android.app.Activity.performCreate(Activity.java: 5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2299)
at android.app.ActivityThread.access$700(ActivityThread.java: 150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1280)
at android.os.Handler.dispatchMessage(Handler.java: 99)
at android.os.Looper.loop(Looper.java: 137)
at android.app.ActivityThread.main(ActivityThread.java: 5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869)
at dalvik.system.NativeStart.main(Native Method)
This is my MainActvity.java :
package com.example.user.samplefragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity implements MenuFragment.OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onItemSelected(String link) {
DetailFragment fragment = (DetailFragment)getFragmentManager().findFragmentById(R.id.detailfragment);
fragment.setText(link);
}
}
This is my Fragment 1 :
package com.example.user.samplefragments;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by USER on 24-04-2016.
*/
public class MenuFragment extends Fragment{
private OnItemSelectedListener listener;
public MenuFragment(){}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu,container,false);
Button btn = (Button)getView().findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateDetail("fake");
}
});
return view;
}
public void updateDetail(String uri){
String newTime = String.valueOf(System.currentTimeMillis());
listener.onItemSelected(newTime);
}
public interface OnItemSelectedListener {
public void onItemSelected(String link);
}
}
This is my Fragment 2 :
package com.example.user.samplefragments;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by USER on 24-04-2016.
*/
public class DetailFragment extends Fragment{
public DetailFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail,container,false);
return view;
}
public void setText(String url){
TextView tv = (TextView)getView().findViewById(R.id.textView);
tv.setText(url);
}
}
This is my activity_main.xml :
Just using two fragments inside the main layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="#+id/menufragment"
class="com.example.user.samplefragments.MenuFragment" /**Declaring the class of the fragment*/
android:layout_height="match_parent"
android:layout_width="5dp">
</fragment>
<fragment
android:id="#+id/detailfragment"
class="com.example.user.samplefragments.DetailFragment"
android:layout_height="match_parent"
android:layout_width="5dp">
</fragment>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
</LinearLayout>
As you can see, the error I'm getting :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.samplefragments/com.example.user.samplefragments.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment.
I know this issue occurs when the package name is wrong, but I have given the correct package name. How can I resolve this?
Solution:
You can modify your MenuFragment using follow:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu, container, false);
Button btn = (Button)view.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateDetail("fake");
}
});
return view;
}
Why:
Every time onCreateView is invoked, the view in the fragment is just about to create.At that time there is no view in your fragment.So getView() always returns null invoked in onCreateView. That's why you've got NullPointerException when inflating fragment in your xml.
To avoid NullPointer, try this:
public void updateDetail(String uri){
String newTime = String.valueOf(System.currentTimeMillis());
if ( listener != null ){
listener.onItemSelected(newTime);
}
}
I think This line makes the app crashing,
Button btn = (Button)getView().findViewById(R.id.button);
Replace this line with
Button btn = (Button)view.findViewById(R.id.button);
It will work if your button contains in the fragment xml (R.layout.menu).
I am using Drawer in a Fragment Floating Button whose value is getting null.
When clicking on Drawer item it is getting closed. Can anybody explain why?
Here is my code.
package com.example.mubeen.emei;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class Category_Item extends Fragment {
FloatingActionButton fab;
public Category_Item() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SaveCategory fragment = new SaveCategory();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment1_container, fragment);
fragmentTransaction.commit();
getActivity().setTitle(R.string.Save_Category);
return inflater.inflate(R.layout.fragment_category__item, container, false);
}
}
}
logcat
12-26 05:22:50.952 9530-9530/com.example.mubeen.emei W/PathParser: Points are too far apart 4.000000596046461
12-26 05:22:53.902 9530-9530/com.example.mubeen.emei D/AndroidRuntime: Shutting down VM
12-26 05:22:53.902 9530-9530/com.example.mubeen.emei E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mubeen.emei, PID: 9530
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
at com.example.mubeen.emei.Category_Item.button(Category_Item.java:49)
at com.example.mubeen.emei.Category_Item.onCreateView(Category_Item.java:37)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:106)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:124)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
initialize your FloatingActionButton fab in your code ..
In your onCreateView method use this.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Enter your code here to do something after clicking Floating Actionbar
}
});
I think this may help you.