java.lang.String android.content.Context.getPackageName() - java

I have two file with name
FirstFragment.java
SecondFragment.java
Code of the FirstFragment is
package com.example.application
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
return view;
}
// The method that i want to call from SecondFragment
public void methodFirst() {
Toast.makeText(getContext(), "Method First Clicked", Toast.LENGTH_SHORT).show();
}
}
Now I want to call the methodFirst function in SecondFragment. I have tried the following code on SecondFragment.
Code of the SecondFragment is
package com.example.application
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment {
Private AppCompatButton btnOneSecondFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_second, container, false);
btnOneSecondFragment = (AppCompatButton) view.findViewById(R.id.btn_one_second_fragment);
FirstFragment firstFrag = new FirstFragment();
btnOneSecondFragment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
firstFrag.methodFirst();
}
});
return view;
}
}
But the following error is displaying
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.content.Context.getPackageName()' on a null
object reference

Related

App closes without any prompt on clicking the button

when i click on the "ChangeBudgetBtn" the next activity i.e. BudgetEdit does not open and the application closes without any prompt
package com.moneymgmt.moneymanagementsystem;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class BudgetFragment extends Fragment {
Button ChangeBudgetBtn;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_budget, container,false);
ChangeBudgetBtn = (Button) rootView.findViewById(R.id.ChangeBudgetBtn);
ChangeBudgetBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(),BudgetEdit.class));
}
});
return rootView;
}
}
Replace this line
final View rootView = inflater.inflate(R.layout.fragment_budget, false);
by the following line
final View rootView = inflater.inflate(R.layout.fragment_budget, container, false);
because you forgot to add the container parameter

Can't add intentintegrator in fragment

I'm using zxing library to read qr codes. It works well in MainActivity. Then I want to add that code in a fragment. But intent integrator is not working for this fragment.
This is the code in QrFragment;
package com.myapp.agpj.qr;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.google.zxing.integration.android.IntentIntegrator;
public class QrFragment extends Fragment {
public QrFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_qr, container, false);
Button scan = (Button) v.findViewById(R.id.bQr);
final Fragment activity = this;
scan.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view){
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan QR codes in the school premises");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
return v;
}
}
This outputs an error "IntentIntegator can't applied to android.support.v4.fragment".

Why won't Fragment Transaction work?

I'm writing a simple program in which a two fragments and one activity is used. Both Fragments are displayed (one at a time) within the activity's Frame Layout. The first fragment is a listview that lists items from which the user can select, then the main activity should swap the first fragment with a detail fragment according to the item position determined by a listener within the first fragment. Trouble is, my program won't actually commence the swap. Here's the code for the activity:
package com.example.user.monkeys;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
public class ListActivity extends FragmentActivity implements
monkeyListFragment.OnMonkeySelectedListener {
#Override
public void onCreate(Bundle savedInstanceState) {
Log.i("Activity", "onCreate Pre-Fragment 1");
super.onCreate(savedInstanceState);
setContentView(R.layout.monkey_list_frame);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
monkeyListFragment monkeyList = new monkeyListFragment();
monkeyList.setArguments(getIntent().getExtras());
// Add the fragment to the container
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, monkeyList);
Log.i("Activity", "made it end onCreate");
}
}
#Override
public void onMonkeyItemSelected(int position) {
Log.i("From Activity", "onMonkeyItemSelected");
monkeyDetailsFragment newDetailFrag = new monkeyDetailsFragment();
Bundle args = new Bundle();
args.putInt("itemPosition", position);
newDetailFrag.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newDetailFrag);
transaction.addToBackStack(null);
transaction.commit();
}
}
And the code for the listview fragment:
package com.example.user.monkeys;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class monkeyListFragment extends Fragment{
OnMonkeySelectedListener monkeyCallBack;
private ListView monkeyLV;
private String[] monkeyStrings;
public interface OnMonkeySelectedListener {
public void onMonkeyItemSelected(int position);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("Fragment 1", "Made it to onCreateView");
View view = inflater.inflate(R.layout.list_fragment, container, false);
monkeyLV = (ListView) view.findViewById(R.id.monkeyListView);
monkeyStrings = getResources().getStringArray(R.array.monkey_data_list);
ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, monkeyStrings);
monkeyLV.setAdapter(objAdapter);
AdapterView.OnItemClickListener monkeyListen = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ListFragment-ClckLstnr", "Made it");
monkeyCallBack.onMonkeyItemSelected(position);
}
};
Log.i("Fragment 1", "Made it past OnItemClick Listener");
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
monkeyCallBack = (OnMonkeySelectedListener) activity;
}
catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
}
and finally the code for the detail fragment (this isn't fully fleshed out but it should still swap I believe).
package com.example.user.monkeys;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class monkeyDetailsFragment extends Fragment {
ImageView monkeyPicture;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
Log.i("From Detail Fragment", "Got here");
View view = inflater.inflate(R.layout.monkey_detail_fragment, container,
false);
return view;
}
}
I found your missing line of code for the listview fragment:
monkeyLV.setOnItemClickListener(monkeyListen)

Android Studio | Button onClick in a NavigationDrawer Fragment

I'm struggling with these errors for a few days already, I tried googling it, but unfortunately I can't find any fixes for this.
I'm still a beginner in Java for Android.
So I got a NavigationDrawer with a Fragment, in that fragment i want to put a button which is clickable, but somehow it just doesn't recognize the button and some things while I've got it in my layout.
This is my code:
package nl.c99.c99nlapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Souf on 21-9-2015.
*/
public class First_Fragment extends Fragment {
View MyView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyView = inflater.inflate(R.layout.first_layout, container, false);
return MyView;
View rootView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) rootView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onClick(v);//THIS IS THE METHOD YOU WROTE ON THE ATTACHED CODE!!
}
});
}
}
Screenshot of my project:
First_Fragment Class
http://i.stack.imgur.com/5W2uG.png
Layout of first_fragment:
http://i.imgur.com/ndVmcX2.png
I'm getting the following errors:
Cannot resolve symbol 'OnClickListener' (new OnClickListener)
Method does not override method from its superclass (#Override)
Parameter 'v' is never used. (View v)
Method OnClick is never used (public void onClick)
Cannot resolve symbol 'OnClickListener' (new OnClickListener)
Add this in your imports : import android.view.View.OnClickListener;
You just have to choose one of your views, since you have 2 I don't know why rootView and MyView then your onClickListener() should be :
Button c = (Button) MyView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//your stuff here
}
});
Parameter 'v' is never used. (View v)
Just remove this onClick(v)
Your coude should work if you do this :
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) MyView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Stuff there
}
});
return MyView;
}
All fo your code
package nl.c99.c99nlapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
/**
* Created by Souf on 21-9-2015.
*/
public class First_Fragment extends Fragment {
View MyView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) MyView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Stuff there
}
});
return MyView;
}
}
You must return your rootView at the end of your code, because return means "end that method", so it won't continue the code below it in the same method.
Replace your code with this one:
package nl.c99.c99nlapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class First_Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) rootView.findViewById(R.id.button2);
c.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Write your code here.
}
});
return rootView;
}
}

Dynamically update TextView inside Fragment (ViewPager Tab Layout)

I have a problem with createing a functionality in my app. I would like that the Button in one tab of my application update the TextViews which are located in the other tab of my application. Unfortunately my solutions does not work - throws NPE everytime I touch the Button1. My code:
Tab which should be updated:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SummaryFragment extends Fragment {
TextView textViewOne;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View summary= inflater.inflate(R.layout.fragment_summary, container, false);
textViewOne = (TextView) summary.findViewById(R.id.books);
textViewOne.setText("You have read " + Stats.book + " books");
return summary;
}
public void updateStats() {
textViewOne.setText("You have read " + Stats.book + " books");
}
}
Tab on which the button is located:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class ReadingStarted extends Fragment {
static FirstPageFragmentListener firstPageListener;
public SummaryFragment summaryFragment;
public ReadingStarted() {
}
public ReadingStarted(FirstPageFragmentListener listener) {
firstPageListener = listener;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View started = inflater.inflate(R.layout.fragment_started, container, false);
Button button1 = (Button) started.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stats.book=Stats.book+1;
summaryFragment.updateStats();
}
});
return started;
}
}
Problems in your code:
SummaryFragment doesnot have a empty constructor every fragment requires it
SummaryFragment summaryFragment should be SummaryFragment summaryFragment = new SummaryFragmet();//no initialization done so npe in summaryFragment.updateStats();
Communication between fragments should be done through activity.
See the developer docs about Fragments.

Categories