how can i use intent in ListFragment? - android

I am creating a app.I've used a tablayout with fragments,extended one of the fragments with ListFragment and wanted to give intent inside that fragment,i used the following...
home_fragment.java
package com.example.mymehnat;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
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 home_fragment extends ListFragment implements AdapterView.OnItemClickListener {
ListView lst;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_fragment, container, false);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter adapter = ArrayAdapter.createFromResource(home_fragment.this.getActivity(), R.array.songs, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapter, View view,int position, long l) {
Intent intent = new Intent(home_fragment.this.getActivity(), Lyrics.class);
intent.putExtra("SongName", lst.getItemAtPosition(position).toString());
lst.getContext().startActivity(intent);
}
}
home_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#android:id/list">
</ListView>
</LinearLayout>
but whenever I click any item on the list it doesn't work,shows nothing and prompted me an error...
E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.widget.ListView.getItemAtPosition(int)' on a null object reference
at com.example.mymehnat.home_fragment.onItemClick(home_fragment.java:36) and so on
please help me.
your help is highly appreciated.

You didn't initialize the ListView lst
Try to do like this.
This need to be declared under the OnCreateView
lst = v.findViewById(R.id.list)

Related

ListView onClick insied DialogFragment doesnt work

I have a list view inside a DialogFragment and onClick for the list item doesnt work. Dont know where Im going wrong here. Implemented onItemClick but no use. Code below.
DialogFragment
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class DFragment extends DialogFragment {
ListView mylist;
ArrayList dateitems ;
ArrayList descitems ;
ArrayList locationitems ;
ArrayList listitems ;
public void setList(ArrayList list){
listitems = list;
}
public void setDateList(ArrayList list){
dateitems = list;
}
public void setLocList(ArrayList list){
locationitems = list;
}
public void setDescList(ArrayList list){
descitems = list;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.popup_content, container,
false);
mylist = (ListView) rootView.findViewById(R.id.poplist);
TextView title = (TextView) rootView.findViewById(R.id.dialog_title);
title.setText("Today's Events");
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getActivity(), SupportEventActivity.class);
intent.putExtra("rvEventTitle", listitems.get(position).toString());
intent.putExtra("rvEventDescription", descitems.get(position).toString());
intent.putExtra("rvEventLocation", locationitems.get(position).toString());
intent.putExtra("rvEventTimestamp", dateitems.get(position).toString());
}
});
// Do something else
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.popup_content_row, R.id.poprow_text, listitems);
mylist.setAdapter(adapter); }
Layout file. popup_content.xml
<?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="wrap_content"
android:orientation="vertical"
android:background="#drawable/back"
>
<TextView
android:id="#+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="25dp"/>
<ListView
android:id="#+id/poplist"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:divider="#8F8F8F"
android:dividerHeight="2px"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" /> </LinearLayout>
You didn't add startActivity(intent) to your OnClickListener.

Fragment with ListView: NullPointerException on setAdapter

I have the Problem, that I get a NullPointExeption when I want to set a adapter on my ListView.
Before I had the Fragment extended with ListFragment and a simple Adapter, that works but the problem was, that I have 3 Fragments in this activity all with ListViews and I got display errors (shows the wrong list in a fragment). So I decided to set for every Fragment own ids on the Listview but now it doesnt work.
Error listview.setAdapter(adapter):
java.lang.NullPointerException at
de.resper.e2cast.MainFragmentLive.onCreateView(MainFragmentLive.java:46)
Fragment:
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import de.resper.e2cast.classes.globalBox;
import de.resper.e2cast.helper.getXml;
import de.resper.e2cast.helper.parseXml;
public class MainFragmentLive extends android.support.v4.app.Fragment {
private List<String> bouquetListString;
private ArrayAdapter<String> adapter;
private globalBox activeBox;
private ListView listview;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_live, container, false);
activeBox = ((globalBox) getActivity().getApplicationContext());
bouquetListString = new ArrayList<String>();
bouquetListString.add("loading...");
if(activeBox.isInit()){
if(activeBox.getBouquets().size() > 0 && activeBox.getBouquets().get(2).size() > 0){
bouquetListString = activeBox.getBouquets().get(2);
}else{
Log.d("Load Bouquet", "XML");
getBouquetBox();
}
}
listview = (ListView) getActivity().findViewById(R.id.listLive);
adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, bouquetListString);
listview.setAdapter(adapter);
ImageButton reloadBouquet = (ImageButton) view.findViewById(R.id.reloadBouquet);
reloadBouquet.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
getBouquetBox();
}
});
setHasOptionsMenu(true);
return view;
}
public void getBouquetBox(){
getXml.DownloadCompleteListener dcl = new getXml.DownloadCompleteListener() {
#Override
public void onDownloadComplete(String result) {
bouquetListString.clear();
String [] tags = {"e2servicereference", "e2servicename"};
List<List<String>> bouquetsList = parseXml.parseXmlByTag(result, tags);
activeBox.addBouquets(bouquetsList);
bouquetListString.addAll(activeBox.getBouquets().get(2));
adapter.notifyDataSetChanged();
}
};
Log.d("MyLogger", "XML Request GET BOUQUET");
getXml downloader = new getXml(dcl);
downloader.execute("http://" + activeBox.getIpPort() + "/web/getservices");
}
}
Fragment XML:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text="#string/selectBouquet"
style="#style/header1"/>
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:id="#+id/reloadBouquet"
android:src="#drawable/ic_action_refresh"
android:contentDescription="#string/search"
android:layout_weight=".20"
android:layout_gravity="bottom"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listLive" />
</LinearLayout>
Use view instead of getActivity() to initializing ListView because ListView is inside Fragment layout instead of Activity :
listview = (ListView) view.findViewById(R.id.listLive);

How do you inflate dynamically created objects in a fragment?

I've looked at most of the other threads but I can't figure out what is causing my application to crash. I keep getting "attempt to invoke virtual method Android.view.View android.view.View.findViewById(int) on a null reference object. This error occurs at line 25 or
"View ObjectView = inflater.inflate(R.layout.fragment_layout_biking,container,false); "
How do you inflate a view for a newly created object in Android?
Thank you.
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View ObjectView = inflater.inflate(R.layout.fragment_layout_biking, container, false);
final EditText input1 = (EditText)getView().findViewById(R.id.input);
final ScrollView scroll = (ScrollView)getView().findViewById(R.id.scrolling);
Button addbox = (Button)getView().findViewById(R.id.CheckBoxAdd);
addbox.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
for (int box = 0; box <25; box++){
CheckBox newcheckbox = (CheckBox)getView().findViewById(R.id.NewCheckBox);
newcheckbox.setText(input1.toString());
scroll.addView(newcheckbox);
}
}
});
return ObjectView;
}
}
Here is the xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearlayout"
android:orientation="vertical" >
<EditText
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
<Button
android:id="#+id/CheckBoxAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Checkbox" />
<ScrollView
android:id="#+id/scrolling"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearlayout2"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
getView returns the View you inflated in onCreateView, before it returns null. In your code you have two options, the first is to use ObjectView.findViewById, to look for the view you need. The latter is to override onViewCreated, and use the first parameter, which in turn is the view you returned in onCreateView, to look for the view you need
Here is the final code that produces the desired result.
package fragments_for_app;
import android.app.adventureprototype.R;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
public class Fragment1 extends Fragment {
LinearLayout layout;
CheckBox newcheckbox;
//18
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View ObjectView = inflater.inflate(R.layout.fragment_layout_biking, container, false);
final EditText input1 = (EditText)ObjectView.findViewById(R.id.input);
layout = (LinearLayout)ObjectView.findViewById(R.id.linearlayout2);
//final ScrollView scroll = (ScrollView)ObjectView.findViewById(R.id.scrolling);
final Button addbox = (Button)ObjectView.findViewById(R.id.CheckBoxAdd);
layout.removeAllViews();
addbox.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
newcheckbox = new CheckBox(ObjectView.getContext());
newcheckbox.setText(input1.getText().toString());
layout.addView(newcheckbox);
//work on this thursday
}
});
return ObjectView;
}
}

Refresh Fragment View inside itself

I have a big request for any of You. I would like to create a third Fragment in my simple application. Everything works fine, no error. I would like to make the last Fragment it is: "TestFragment" when I click that button inside that Fragment I'd like to refresh its View (on click button) and load another view inside this fragment (the same fragment). Please help me do this, show how to get it. I have read in Google but can not find the solution appropriate for this application.
Here is simple code:
MAIN ACTIVITY:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
private MyAdapter mAdapter;
private ViewPager mPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 2;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TestFragment();
// case 1:
// return new ImageFragment();
case 1:
return new TestFragment();
//
default:
return null;
}
}
}
}
TEST FRAGMENT:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
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.TextView;
public class TestFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test, container, false);
// TextView textView = (TextView) view.findViewById(R.id.detailsText);
// textView.setText("Testing");
return view;
}
}
TEST LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QUIZ IMPORTANT TAKE A LOOK AT THIS!!!!!"
android:textSize="30dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginLeft="30dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdjiasdjasoldjasjdasjdlasjdksahfkadshfalidshfladshfladsfhlasdfgjl;adkglkafdgnklfdsgjpfasmga.dsnvlkasdnvkldsnflkdasjflkasdnvlkadjvodsijfosdjv;lasdvlasdjvladosijao" />
DETAILS FRAGMENT:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
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.TextView;
public class Details extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
TextView textView = (TextView) view.findViewById(R.id.detailsText);
textView.setText("Testing");
return view;
}
}
ACTIVITY_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:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
DETAILS LYOUT:
<?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:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
How to do this refresh inside the same fragment?

List View With Sliding Menu Fragemnt

Update:
Just so you know, when i try importing the project I end up with more errors than i can count thus why i'm here trying to figure it out. My sliding was working fine, it was just adding a listmenu that was throwing me off. Thank you for being patient with me.
I didn't change the main so here it is anyways:
package com.projectcaruso.nfp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
}
I tired to import his code into mine as the MenuFragment.java
package com.projectcaruso.nfp;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MenuFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
for (int i = 0; i < 20; i++) {
adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search));
}
setListAdapter(adapter);
}
private class SampleItem {
public String tag;
public int iconRes;
public SampleItem(String tag, int iconRes) {
this.tag = tag;
this.iconRes = iconRes;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
}
ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView.findViewById(R.id.row_title);
title.setText(getItem(position).tag);
return convertView;
}
}
}
The list.xml is blank
the activity_main.xml is as follows:
<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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
There are many problems with your current code. I suggest you look at the sample given in Sliding menu (SlidingFragmentActivity,SlidingListActivity) examples
You CAN NOT have two Activities running at the same time. Sliding menu is in the back, but it is managed by the front activity. What you are looking for is a Fragment.
Update:
Activity, ListFragment samples may help.
Basiclly what you need is
// set the content view
setBehindContentView(R.layout.fragment_menu);
Should be a blank layout. Then the important part:
return inflater.inflate(R.layout.list, null);
If you do super.onCreateView() that will just do nothing or crash like what you having now. I strongly recommend you looking at the two samples in the link.

Categories

Resources