Passing data between the fragments - java

I am trying to pass the data from edittext of one fragment to textview associated with other fragment
I am following static way of passing data between fragments
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/fragment2"
android:name="com.example.usingtwofragmentsfornavigatingbetweentwoactivities.Fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="#+id/fragment1"
android:name="com.example.usingtwofragmentsfornavigatingbetweentwoactivities.Fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Click to send data to next Activity" />
</RelativeLayout>
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView"
android:textSize="40dp" />
</RelativeLayout>
Fragment1.java
package com.example.usingtwofragmentsfornavigatingbetweentwoactivities;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Fragment1 extends Fragment{
Button button;
EditText textedit;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.fragment_1, container, false);
button=(Button) view.findViewById(R.id.button1);
textedit=(EditText) view.findViewById(R.id.editText1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager FM = getFragmentManager();
Fragment2 f2=(Fragment2) FM.findFragmentById(R.id.fragment2);
f2.setName(textedit.getText().toString());
}
});
return view;
}
}
Fragment2.java
package com.example.usingtwofragmentsfornavigatingbetweentwoactivities;
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 Fragment2 extends Fragment{
View view;
TextView txtview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.fragment_2, container, false);
return view;
}
public void setName(String Name){
txtview=(TextView) view.findViewById(R.id.textView1);
txtview.setText("Hello:: " +Name);
}
}
[Edit]
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/fragment2"
class="com.example.usingtwofragmentsfornavigatingbetweentwoactivities.Fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="#+id/fragment1"
class="com.example.usingtwofragmentsfornavigatingbetweentwoactivities.Fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Fragment1.java
package com.example.usingtwofragmentsfornavigatingbetweentwoactivities;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Fragment1 extends Fragment{
Button button;
EditText textedit;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.fragment_1, container, false);
button=(Button) view.findViewById(R.id.button1);
textedit=(EditText) view.findViewById(R.id.editText1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager FM = getActivity().getSupportFragmentManager();
Fragment2 f2=(Fragment2) FM.findFragmentById(R.id.fragment2);
f2.setName(textedit.getText().toString());
}
});
return view;
}
}
Log::
10-30 14:37:32.900: D/AndroidRuntime(460): Shutting down VM
10-30 14:37:32.900: W/dalvikvm(460): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-30 14:37:32.940: E/AndroidRuntime(460): FATAL EXCEPTION: main
10-30 14:37:32.940: E/AndroidRuntime(460): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usingtwofragmentsfornavigatingbetweentwoactivities/com.example.usingtwofragmentsfornavigatingbetweentwoactivities.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.os.Looper.loop(Looper.java:123)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-30 14:37:32.940: E/AndroidRuntime(460): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 14:37:32.940: E/AndroidRuntime(460): at java.lang.reflect.Method.invoke(Method.java:521)
10-30 14:37:32.940: E/AndroidRuntime(460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-30 14:37:32.940: E/AndroidRuntime(460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-30 14:37:32.940: E/AndroidRuntime(460): at dalvik.system.NativeStart.main(Native Method)
10-30 14:37:32.940: E/AndroidRuntime(460): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-30 14:37:32.940: E/AndroidRuntime(460): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.Activity.setContentView(Activity.java:1647)
10-30 14:37:32.940: E/AndroidRuntime(460): at com.example.usingtwofragmentsfornavigatingbetweentwoactivities.MainActivity.onCreate(MainActivity.java:12)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-30 14:37:32.940: E/AndroidRuntime(460): ... 11 more
10-30 14:37:32.940: E/AndroidRuntime(460): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.usingtwofragmentsfornavigatingbetweentwoactivities-2.apk]
10-30 14:37:32.940: E/AndroidRuntime(460): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
10-30 14:37:32.940: E/AndroidRuntime(460): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
10-30 14:37:32.940: E/AndroidRuntime(460): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
10-30 14:37:32.940: E/AndroidRuntime(460): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
10-30 14:37:32.940: E/AndroidRuntime(460): ... 20 more
MainActivity.java
package com.example.usingtwofragmentsfornavigatingbetweentwoactivities;
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;
}
}
How to resolve this ?

java.lang.ClassNotFoundException: android.view.fragment
you are not this importing fragment from the support library and your device is older pre Android 3. It should be
import android.support.v4.app.Fragment;
also in your xml change
android:name
with
class

Inside Fragment1 onclick of button1, do something like this:
FragmentManager FM = getActivity().getSupportFragmentManager();
Fragment2 f2=(Fragment2) FM.findFragmentById(R.id.fragment2);
f2.setName(textedit.getText().toString());
I hope it will be helpful !!

Related

android.view.InflateException: Binary XML file line #16: Error inflating class <unknown> (ListView Maybe?) [duplicate]

This question already exists:
android.view.InflateException: Binary XML file line #16: Error inflating class fragment
Closed 6 years ago.
I made an app, it works well in virtual device and real device, but today my app is throwing exceptions, maybe it is the same with some old errors in here, but I can not find my own answer for this. So, please tell me how to solve it, thanks so much
here is my logcat
FATAL EXCEPTION: main
Process: com.gvc.tvschedule, PID: 1918
android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:121)
at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
... 48 more
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x12/d=0x0 a=2 r=0x7f0b0068}
at android.content.res.Resources.loadDrawable(Resources.java:2073)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.TextView.<init>(TextView.java:806)
at android.widget.TextView.<init>(TextView.java:618)
... 51
here under is Layout XML: getprogram_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/relativeLayoutProgram"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayoutProgram"
android:orientation="vertical"
android:layout_marginTop="40dp">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
</ListView>
<!-- #android:id/list or #id/android:list -->
</RelativeLayout>
</RelativeLayout>
and: view_program_entry.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="horizontal" >
<TextView
android:id="#+id/programtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="15sp"
android:paddingLeft="6sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/programtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:drawableLeft="#+id/programtime" />
<!-- android:background="#color/blue2" -->
</LinearLayout>
and Java : ProgramPickerActivity.java
package com.gvc.tvschedule;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.gvc.service.DBController;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ProgramPickerActivity extends Activity {
// DB Class to perform DB related operations
DBController controller = new DBController(this);
// Progress Dialog Object
ProgressDialog prgDialog;
HashMap<String, String> queryValues;
private static String titleTextFromView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.getprogram_main);
ArrayList<HashMap<String, String>> programList = controller.generalProgram(MainActivity.getNameOfChannel(), DatePickerActivity.getDate());
if (programList.size() != 0) {
// Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), programList, R.layout.view_program_entry, new String[] {
"ptime", "ptitle" }, new int[] { R.id.programtime, R.id.programtitle });
ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
TextView titleVIew = (TextView) view.findViewById(R.id.programtitle);
titleTextFromView = titleVIew.getText().toString();
createPopupWindownForDescription(titleTextFromView);
}
});
}
prgDialog = new ProgressDialog(this);
prgDialog.setMessage("loading...");
prgDialog.setCancelable(false);
}
private void createPopupWindownForDescription(String pTitle){
String content = controller.getDescription(pTitle);
AlertDialog.Builder builder = new AlertDialog.Builder(ProgramPickerActivity.this);
builder.setTitle("Content");
builder.setMessage(content);
builder.show();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(prgDialog!=null)
prgDialog.dismiss();
}
public static String getTitleTextFromView(){
return titleTextFromView;
}
}
I think the trouble is related with the listview, because, when no data put on listview, it is fine, but with data, it threw the errors.
I believe your current problem is
<TextView
android:id="#+id/programtitle"
...
android:drawableLeft="#+id/programtime" /> // HERE
You are referencing the other TextView but you should be referencing a Drawable. My guess is that you want to put it to the left of the other TextView?
If this is the case, the LinearLayout will lay them out from left to right by default so you just need to put them in the order that you want them to appear. Also, not a problem but since they are left to right by default, there's no need for
android:orientation="horizontal"
It's not showing when you don't have any items in your ListView because the layout is never inflated so the error is never caught.
Docs
android:id="#+id/list"
use that, you're welcome
PS: Change
ListView myList = (ListView) findViewById(android.R.id.list);
for:
ListView myList = (ListView) findViewById(R.id.list);

Unable to start activity: NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
Hi I am encountering a null pointer error when I click on the activity Links. The purpose of my feature is to call a list of links in the form of list view. Below is the logcat and as far as I understand it is coming from my onItemClickListener, but I can't seem to point out the null error.:
sitesList.setOnItemClickListener(new OnItemClickListener()
Links.java
package com.example.sgrecipe;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class Links extends Activity {
private SitesAdapter mAdapter;
private ListView sitesList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("StackSites", "OnCreate()");
setContentView(R.layout.activity_main);
//Get reference to our ListView
sitesList = (ListView)findViewById(R.id.sitesList);
//Set the click listener to launch the browser when a row is clicked.
sitesList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
String url = mAdapter.getItem(pos).getLink();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
/*
* If network is available download the xml from the Internet.
* If not then try to use the local file from last time.
*/
if(isNetworkAvailable()){
Log.i("StackSites", "starting download Task");
SitesDownloadTask download = new SitesDownloadTask();
download.execute();
}else{
mAdapter = new SitesAdapter(getApplicationContext(), -1, SitesXmlPullParser.getStackSitesFromFile(Links.this));
sitesList.setAdapter(mAdapter);
}
}
//Helper method to determine if Internet connection is available.
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
/*
* AsyncTask that will download the xml file for us and store it locally.
* After the download is done we'll parse the local file.
*/
private class SitesDownloadTask extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... arg0) {
//Download the file
try {
Downloader.DownloadFromUrl("https://dl.dropboxusercontent.com/u/5724095/XmlParseExample/stacksites.xml", openFileOutput("StackSites.xml", Context.MODE_PRIVATE));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result){
//setup our Adapter and set it to the ListView.
mAdapter = new SitesAdapter(Links.this, -1, SitesXmlPullParser.getStackSitesFromFile(Links.this));
sitesList.setAdapter(mAdapter);
Log.i("StackSites", "adapter size = "+ mAdapter.getCount());
}
}
}
activity_links.xml
<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"
tools:context=".Links" >
<ListView
android:id="#+id/sitesList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
row_site.xml (for the listview)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" >
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/iconImg"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="8dp" />
<TextView
android:id="#+id/nameTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/iconImg"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/aboutTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/iconImg"
android:textSize="12sp"
android:layout_below="#id/nameTxt"
/>
</RelativeLayout>
Here is the logcat:
02-23 15:13:10.856: E/AndroidRuntime(8201): FATAL EXCEPTION: main
02-23 15:13:10.856: E/AndroidRuntime(8201): Process: com.example.sgrecipe, PID: 8201
02-23 15:13:10.856: E/AndroidRuntime(8201): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sgrecipe/com.example.sgrecipe.Links}: java.lang.NullPointerException
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread.access$800(ActivityThread.java:163)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.os.Handler.dispatchMessage(Handler.java:102)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.os.Looper.loop(Looper.java:157)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread.main(ActivityThread.java:5335)
02-23 15:13:10.856: E/AndroidRuntime(8201): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 15:13:10.856: E/AndroidRuntime(8201): at java.lang.reflect.Method.invoke(Method.java:515)
02-23 15:13:10.856: E/AndroidRuntime(8201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-23 15:13:10.856: E/AndroidRuntime(8201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-23 15:13:10.856: E/AndroidRuntime(8201): at dalvik.system.NativeStart.main(Native Method)
02-23 15:13:10.856: E/AndroidRuntime(8201): Caused by: java.lang.NullPointerException
02-23 15:13:10.856: E/AndroidRuntime(8201): at com.example.sgrecipe.Links.onCreate(Links.java:36)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.Activity.performCreate(Activity.java:5389)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-23 15:13:10.856: E/AndroidRuntime(8201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
Any help is really appreciate, thank you!
Change following line
setContentView(R.layout.activity_main);
to
setContentView(R.layout.activity_links);
Seems like your using different layouts:
setContentView(R.layout.activity_main);
vs
activity_links.xml

Issue with Activities and ListView on Android

I'm new in the world of Android and I have a problem with the use of activities, my problem that I want to make an app using ListView to show a list of categories of business and every business category will has another list with the names of the companies.
My array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="categorias">
<item >Agencias de Autos y Motos</item>
<item >Agroinsumos</item>
<item >Audio Car</item>
<item >Arquitectura y Construcción</item>
<item >Autolavados</item>
<item >Autopartes</item>
<item >Balnearios</item>
</string-array>
</resources>
This is my MainActivity and its layout:
package com.genebi.rubik;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.app.ListActivity;
import android.content.Intent;
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] lista_categorias = getResources().getStringArray(R.array.categorias);
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.lista_categorias, R.id.nombreCategoria, lista_categorias));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
String categoria = ((TextView) v).getText().toString();
Intent i = new Intent(getApplicationContext(), ListaPorCategoria.class);
i.putExtra("categoria", categoria);
startActivity(i);
}
});
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/nombreCategoria"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip"
android:textSize="14sp"
android:textStyle="normal"
/>
</RelativeLayout>
And my problem starts here with the second Activity when i give click on a item of the list showed in MainActivity.class
package com.genebi.rubik;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
public class ListaPorCategoria extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_por_categoria);
TextView txtCategoria = (TextView) findViewById(R.id.etiquetaCategoria);
Intent i = getIntent();
String categoriaSeleccionada = i.getStringExtra("categoria");
txtCategoria.setText(categoriaSeleccionada);
}
}
Layout
<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=".ListaPorCategoria" >
<TextView android:id="#+id/etiquetaCategoria"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:padding="10dip"
android:textColor="#ffffff"/>
</RelativeLayout>
The application just stop working and I can't see the info of the category that i clicked, apparently it has not errors but i have no idea why is not working, i hope you can help me, thank you.
EDIT
Logcat
06-05 13:45:14.667: D/OpenGLRenderer(20800): Enabling debug mode 0
06-05 13:45:19.692: D/AbsListView(20800): Get MotionRecognitionManager
06-05 13:45:19.732: I/Adreno200-EGLSUB(20800): <ConfigWindowMatch:2087>: Format RGBA_8888.
06-05 13:45:39.671: D/AndroidRuntime(20800): Shutting down VM
06-05 13:45:39.671: W/dalvikvm(20800): threadid=1: thread exiting with uncaught exception (group=0x40e40360)
06-05 13:45:39.671: E/AndroidRuntime(20800): FATAL EXCEPTION: main
06-05 13:45:39.671: E/AndroidRuntime(20800): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
06-05 13:45:39.671: E/AndroidRuntime(20800): at com.genebi.rubik.MainActivity$1.onItemClick(MainActivity.java:31)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.widget.AdapterView.performItemClick(AdapterView.java:301)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3071)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.widget.AbsListView$1.run(AbsListView.java:3973)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.os.Handler.handleCallback(Handler.java:615)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.os.Looper.loop(Looper.java:137)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.app.ActivityThread.main(ActivityThread.java:4950)
06-05 13:45:39.671: E/AndroidRuntime(20800): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 13:45:39.671: E/AndroidRuntime(20800): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 13:45:39.671: E/AndroidRuntime(20800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-05 13:45:39.671: E/AndroidRuntime(20800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-05 13:45:39.671: E/AndroidRuntime(20800): at dalvik.system.NativeStart.main(Native Method)
this is wrong way for getting value from clicked item on listView:
String categoria = ((TextView) v).getText().toString();
as v is your row in list you can't cast that to TextView and get text from that,
you need use following code:
TextView c = (TextView) v.findViewById(R.id.TextViewID); // i think this is nombreCategoria
String categoria = c.getText().toString();
or you can use position for getting value from list, like following code:
String categoria = lista_categorias[position]
and another way is:
String categoria =(String) arg0.getItemAtPosition(position);

Crash when adding a OnClickListener to a Button

I'm a total newbie with Android, and I'm trying to switch between two activities ("MainActivity" and "LoginDisplayActivity") and adding the "OnClickListener" seem to make my app crash on startup.
Here is my code :
package info.dremor.kronos;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new MainFragment()).commit();
}
final Button loginButton = (Button) findViewById(R.id.connect);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, LoginDisplayActivity.class);
startActivity(intent);
}
});
}
#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;
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class MainFragment extends Fragment {
public MainFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
trace :
05-26 15:48:11.286: D/AndroidRuntime(947): Shutting down VM
05-26 15:48:11.286: W/dalvikvm(947): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
05-26 15:48:11.296: E/AndroidRuntime(947): FATAL EXCEPTION: main
05-26 15:48:11.296: E/AndroidRuntime(947): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.dremor.kronos/info.dremor.kronos.MainActivity}: java.lang.NullPointerException
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.os.Looper.loop(Looper.java:137)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-26 15:48:11.296: E/AndroidRuntime(947): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 15:48:11.296: E/AndroidRuntime(947): at java.lang.reflect.Method.invoke(Method.java:511)
05-26 15:48:11.296: E/AndroidRuntime(947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-26 15:48:11.296: E/AndroidRuntime(947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-26 15:48:11.296: E/AndroidRuntime(947): at dalvik.system.NativeStart.main(Native Method)
05-26 15:48:11.296: E/AndroidRuntime(947): Caused by: java.lang.NullPointerException
05-26 15:48:11.296: E/AndroidRuntime(947): at info.dremor.kronos.MainActivity.onCreate(MainActivity.java:31)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.Activity.performCreate(Activity.java:5008)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-26 15:48:11.296: E/AndroidRuntime(947): ... 11 more
activity_main.xml :
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.dremor.kronos.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml :
<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="info.dremor.kronos.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp"
android:text="#string/connect" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="#string/login_greeting" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="#string/login" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:ems="10" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="#string/password" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/textView2"
android:layout_marginTop="35dp"
android:ems="10"
android:inputType="textPassword" />
</RelativeLayout>
Can someone help me?
In your activity, you set the layout file like this:
setContentView(R.layout.activity_main);
This means, when you write findViewById, it will look within the activity_main.xml file.
Your button on the other hand is within another layout file.
Just move the click event into your fragment, and access the context using getActivity like so:
final Button loginButton = (Button) rootView.findViewById(R.id.connect);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), LoginDisplayActivity.class);
startActivity(intent);
}
});
your button is in the fragment layout, you're "finding" it from the activity layout. Setup button onclick in the fragment onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
final Button loginButton = (Button) rootView.findViewById(R.id.connect);
loginButton.setOnClickListener(new OnClickListener(){...})
return rootView;
}
Remove the button and code from onCreate and shift the button and clicklistener code to onCreateView like this:
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
final Button loginButton = (Button) rootView.findViewById(R.id.connect);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, LoginDisplayActivity.class);
startActivity(intent);
}
});
return rootView;
As your button is in fragment_main, not activity_main. The button is not found.

NullPointException in ListView

Please have a look at the following code
activity_form.xml
<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"
tools:context=".Form" >
<ImageView
android:id="#+id/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/logo" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/logo"
android:layout_marginTop="10dp"
android:text="#string/title" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
list_layout.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:orientation="horizontal" >
<ImageView
android:id="#+id/listImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/star" />
<TextView
android:id="#+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Form.java
package com.example.callmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Form extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
ListView lv = (ListView)findViewById(android.R.id.list);
String arr[] = getResources().getStringArray(R.array.branch_list);
lv.setAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.listText,arr));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_form, menu);
return true;
}
private class MyAdapter extends ArrayAdapter
{
public MyAdapter(Context context, int resource, int textViewResourceId,
Object[] objects) {
super(context, resource, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View contentView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_layout, parent,false);
TextView tv = (TextView)findViewById(R.id.listText);
ImageView iv = (ImageView)findViewById(R.id.listImage);
String arr[] = getResources().getStringArray(R.array.branch_list);
tv.setText(arr[position]);
if(arr[position].equals("Anuradhapura"))
iv.setImageResource(R.drawable.star);
return view;
}
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Esoft Call Manager</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title"><b>Select a Branch To Call</b></string>
<string-array name="branch_list">
<item>Anuradhapura</item>
<item>Avissawella</item>
</string-array>
</resources>
When I run this code, I get the 'NullPointException'. I am adding the complete log file here
02-08 21:04:01.463: D/dalvikvm(490): GC_EXTERNAL_ALLOC freed 43K, 53% free 2551K/5379K, external 1949K/2137K, paused 82ms
02-08 21:04:01.713: D/AndroidRuntime(490): Shutting down VM
02-08 21:04:01.713: W/dalvikvm(490): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-08 21:04:01.733: E/AndroidRuntime(490): FATAL EXCEPTION: main
02-08 21:04:01.733: E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.callmanager/com.example.callmanager.Form}: java.lang.NullPointerException
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.os.Looper.loop(Looper.java:123)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-08 21:04:01.733: E/AndroidRuntime(490): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 21:04:01.733: E/AndroidRuntime(490): at java.lang.reflect.Method.invoke(Method.java:507)
02-08 21:04:01.733: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-08 21:04:01.733: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-08 21:04:01.733: E/AndroidRuntime(490): at dalvik.system.NativeStart.main(Native Method)
02-08 21:04:01.733: E/AndroidRuntime(490): Caused by: java.lang.NullPointerException
02-08 21:04:01.733: E/AndroidRuntime(490): at com.example.esoftcallmanager.Form.onCreate(Form.java:24)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-08 21:04:01.733: E/AndroidRuntime(490): ... 11 more
I am adding the folder structure below if it helps
Why I am getting this error? How to solve this? Please help!
try this:
change this
ListView lv = (ListView)findViewById(android.R.id.list);
for:
ListView lv = (ListView)findViewById(R.id.list);
Normally they use android.R.id.list when class extends a ListActivity
Change:
ListView lv = (ListView)findViewById(android.R.id.list);
to
ListView lv = (ListView)findViewById(R.id.list);
Unless you have an id in your XML in the format of android:id="#android:id/myID", you don't need to use android.R. Whenever you use one of your own IDs, you can simply use R.id.Whatever
The issue is right here. ListView lv = (ListView)findViewById(android.R.id.list); you reference the Id android.R.id.list
however in your layout you reference android:id="#+id/list" which creates an Id in the Resources class called list. so you should be referencing R.id.list if you debug you will find that the lv variable is null because it wasn't found because you referenced the wrong id.
If your are using your own layout you don't need any more to write android.R just remove android and reorganize your class
Try this.
I hope it will solve your NPE.
Change it
ListView lv = (ListView)findViewById(android.R.id.list);
to
ListView lv = (ListView)findViewById(R.id.list);
Actually You have defined the list view in the layout activity_form.xml have name list so you need to write the R.id.list.
I managed to solve this by my self. Following edits were required.
activity_form.xml
The "id" of the ListView should be
android:id="#android:id/list"
Form.java
It should call the ListView in this way
ListView lv = (ListView) findViewById(android.R.id.list);
Inside getView(), the initialization of the TextView and ImageView should be like this
TextView tv = (TextView) view.findViewById(R.id.listText);
ImageView iv = (ImageView) view.findViewById(R.id.listImage);
Note the view.findViewById() part.

Categories