PullToRefreshGridView Inflate Exception - java

I am using this library https://github.com/chrisbanes/Android-PullToRefresh for implementing a refreshable grid view and i am getting this error:
android.view.InflateException: Binary XML file line #8: Error inflating class com.handmark.pulltorefresh.library.PullToRefreshGridView
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at com.example.retrievetweets.FragmentPhotos.onCreateView(FragmentPhotos.java:55)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:238)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
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:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 24 more
Caused by: java.lang.NullPointerException
at com.handmark.pulltorefresh.library.internal.IndicatorLayout.<init>(IndicatorLayout.java:66)
at com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.addIndicatorViews(PullToRefreshAdapterViewBase.java:355)
at com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.updateUIForMode(PullToRefreshAdapterViewBase.java:328)
at com.handmark.pulltorefresh.library.PullToRefreshBase.init(PullToRefreshBase.java:1142)
at com.handmark.pulltorefresh.library.PullToRefreshBase.<init>(PullToRefreshBase.java:113)
at com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.<init>(PullToRefreshAdapterViewBase.java:74)
at com.handmark.pulltorefresh.library.PullToRefreshGridView.<init>(PullToRefreshGridView.java:35)
... 27 more
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- The PullToRefreshGridView replaces a standard GridView widget. -->
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/pull_refresh_grid"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="1dp"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:gravity="fill"
ptr:ptrMode="both"
ptr:ptrDrawable="#drawable/default_ptr_rotate"
/>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:indeterminateOnly="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
And here is the java code for the method onCreateView in FragmentPhotos (the line 55 is this one "ll = (RelativeLayout) inflater.inflate(R.layout.activity_grid_view,"):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ref = "";
fa = getActivity();
ll = (RelativeLayout) inflater.inflate(R.layout.activity_grid_view,
container, false);
bar = (ProgressBar) ll.findViewById(R.id.progressBar);
mPullRefreshGridView = (PullToRefreshGridView) ll
.findViewById(R.id.pull_refresh_grid);
mGridView = mPullRefreshGridView.getRefreshableView();
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshGridView
.setOnRefreshListener(new OnRefreshListener<GridView>() {
#Override
public void onRefresh(
PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
if (NetworkReceiver.mobileConnected) {
new GetDataTaskWhitoutLoading().execute();
} else {
Toast.makeText(
getActivity(),
"Ahora mismo no se pueden cargar nuevos datos."
+ " Comprueba la conexi—n a Internet.",
Toast.LENGTH_LONG).show();
mPullRefreshGridView.onRefreshComplete();
}
}
});
mGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// Do something in response to the click
Intent intent = new Intent(fa, ImageSelected.class);
intent.putExtra("URL_image", mListItems.get(position)
.subSequence(0, mListItems.get(position).length() - 6));
startActivity(intent);
}
});
mListItems = new ArrayList<String>();
mAdapter = new FotosItemAdapter(fa, R.layout.image_group, mListItems);
mGridView.setAdapter(mAdapter);
if (NetworkReceiver.mobileConnected) {
GetDataTask.newInstance(bar).execute();
}
return ll;
}
Thank you!!

Are you importing in your classhpath on eclipse?
Pull to refhresh is a lib for android,then import like this:
project properties> android> add lib.

In my case it solved by adding this to my project.properties :
target=android-19
android.library.reference.1=../library
that library is the name of the handmark library.
hope be useful.

Related

Error when trying to add splash screen - android app

I'm an android newbie
I was following this tutorial Android splash screen howto, it's a little outdated, but anyways.
I just add the new layout file splash.xml into res/layout, and then the image into drawable/mdpi, then I change the main image into MainActivity.java, ie:
setContentView(R.layout.activity_main);
to
setContentView(R.layout.splash);
Being activity_main the name of the default layout, activity_main.xml, and splash the name of the other layout called splash.xml.
But application stops, says the application has unfortunately stopped, and I can't even access it.
I don't know what am I missing here, here's the interesting code on MainActivity.java:
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
relativeLayout.setDrawingCacheEnabled(true);
cameraSurfaceView = (SurfaceView)
findViewById(R.id.surfaceView1);
// cameraSurfaceView.setLayoutParams(new FrameLayout.LayoutParams(640, 480));
cameraSurfaceHolder = cameraSurfaceView.getHolder();
cameraSurfaceHolder.addCallback(this);
// cameraSurfaceHolder.setType(SurfaceHolder.
// SURFACE_TYPE_PUSH_BUFFERS);
btnCapture = (Button)findViewById(R.id.button1);
btnCapture.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
camera.takePicture(cameraShutterCallback,
cameraPictureCallbackRaw,
cameraPictureCallbackJpeg);
}
});
}
My activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/containerImg"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="1276px"
android:layout_height="745px"
android:layout_centerInParent="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/surfaceView1"
android:layout_alignLeft="#+id/surfaceView1"
android:layout_marginLeft="20px"
android:src="#drawable/mark3" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:background="#drawable/camera" />
</FrameLayout>
My splash.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="fill_parent">
<ImageView
android:src="#drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
</LinearLayout>
Any ideas?
Maybe I should use the same activity_main.xml for this purposes, not really sure...
Thanks in advance!
EDIT
Logcat:
3774-3774/com.kkoci.photo E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kkoci.photo/com.kkoci.photo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
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:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
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.kkoci.photo.MainActivity.onCreate(MainActivity.java:65)
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:2295)
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:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
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)
relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
relativeLayout.setDrawingCacheEnabled(true);
cameraSurfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
these are the views present in your activity_main xml file.. when you change your layout file why you are accessing these?

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.

Android application has stopped working -eclipse

I have been tried to build my first Android Application. Every time I open the application in the emulator, I just get a message that says test has stopped working. It must be something simple. Hope you can help me.
04-06 15:43:47.806: W/dalvikvm(4275): threadid=1: thread exiting with
uncaught exception (group=0xa614d908)
04-06 15:43:47.826: E/AndroidRuntime(4275): FATAL EXCEPTION: main
04-06 15:43:47.826: E/AndroidRuntime(4275):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.test/com.test.MainActivity}:
java.lang.NullPointerException
04-06 15:43:47.826: E/AndroidRuntime(4275): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
android.app.ActivityThread.access$600(ActivityThread.java:141)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
android.os.Handler.dispatchMessage(Handler.java:99)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
android.os.Looper.loop(Looper.java:137)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
android.app.ActivityThread.main(ActivityThread.java:5041)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
java.lang.reflect.Method.invokeNative(Native Method)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
java.lang.reflect.Method.invoke(Method.java:511)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
dalvik.system.NativeStart.main(Native Method)
04-06 15:43:47.826: E/AndroidRuntime(4275): Caused by:
java.lang.NullPointerException
04-06 15:43:47.826: E/AndroidRuntime(4275): at
com.test.MainActivity.onCreate(MainActivity.java:30)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
android.app.Activity.performCreate(Activity.java:5104)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-06 15:43:47.826: E/AndroidRuntime(4275): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-06 15:43:47.826: E/AndroidRuntime(4275): ... 11 more
Above is the log file
public class Main extends ActionBarActivity {
int counter ;
Button add, sub ;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById (R.id.bAdd);
sub = (Button) findViewById (R.id.bsub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter --;
display.setText("Your total is " + counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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 PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Above is the Main class
<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="com.test.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignLeft="#+id/bsub"
android:layout_alignParentBottom="true"
android:layout_below="#+id/bsub"
android:gravity="center"
android:text="Your total is 0"
android:textSize="45dp" />
<Button
android:id="#+id/badd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="78dp"
android:gravity="center"
android:text="Add one"
android:textSize="20dp" />
<Button
android:id="#+id/bsub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/badd"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Subtract one"
android:textSize="20dp" />
</RelativeLayout>
Above is the fragment_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="com.Hello.firstapp.Main"
tools:ignore="MergeRootFrame" />
ABove is the activity_main.xml
Instead of onCreate() move all ur initialization into onCreateView() as those buttons and textview is in ur fragment_main.xml thats why u got NPE as those buttons and textview is not the part of activity_main.xml.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
counter = 0;
add = (Button) rootView.findViewById (R.id.bAdd);
sub = (Button) rootView.findViewById (R.id.bsub);
display = (TextView) rootView.findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter --;
display.setText("Your total is " + counter);
}
});
return rootView;
}
I see your issue. activity_main.xml contains a frame layout, in which your fragment_main.xml is inflated. The thing is that, both add and sub are null, as you're looking for them in the wrong layout. Thus, call the FragmentTransaction first and move all your button code to the onCreateView of the PlaceHolderFragment and your issue should be fixed. I have provided the fixed code, all you have to do is copy it and remove the button code from onCreate (...)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
counter = 0;
add = (Button) rootView.findViewById (R.id.bAdd);
sub = (Button) rootView.findViewById (R.id.bsub);
display = (TextView) rootView.findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter --;
display.setText("Your total is " + counter);
}
});
return rootView;
}
Most likely
add = (Button) findViewById (R.id.bAdd);
returns null, because the element is not defined on your activity_main view.

FATAL EXCEPTION: main. (Runtime exception) . Android

I'm new to android. I just tried creating a simple app that performs basic mathematical operations (addition,subtraction,multiplication and division). When i try to run the code i get FATAL EXCEPTION: mainerror. I've gone through the other threads that had the same problem. I still couldn't figure out why i'm having this error.
The mainactivity.java file is
package com.example.mathematicalopr;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button add,sub,mul,div;
TextView display2;
EditText display,display1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//int c=0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
mul = (Button) findViewById(R.id.times);
div = (Button) findViewById(R.id.divide);
display = (EditText) findViewById(R.id.tvDisplay);
String myFirstNum = display.getText().toString();
final int a = Integer.parseInt(myFirstNum);
display1 = (EditText) findViewById(R.id.tvDisplay1);
String mySecondNum = display1.getText().toString();
final int b = Integer.parseInt(mySecondNum);
display2 = (TextView) findViewById(R.id.tvDisplay2);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int c = a+b;
display2.setText("Answer is"+c);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int c = a-b;
display2.setText("Answer is"+c);
}
});
mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int c = a*b;
display2.setText("Answer is"+c);
}
});
div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int c = a/b;
display2.setText("Answer is"+c);
}
});
}
#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;
}
}
My activity_main.xml file is
<?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" >
<EditText
android:id="#+id/tvDisplay"
android:layout_width="342dp"
android:layout_height="wrap_content"
android:inputType="number"
android:text="#string/ref" />
<EditText
android:id="#+id/tvDisplay1"
android:layout_width="342dp"
android:layout_height="wrap_content"
android:inputType="number"
android:text="#string/ref1" />
<Button
android:id="#+id/bAdd"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/add" />
<Button
android:id="#+id/bSub"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/sub" />
<Button
android:id="#+id/times"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/times" />
<Button
android:id="#+id/divide"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/divide" />
<TextView
android:id="#+id/tvDisplay2"
android:layout_width="342dp"
android:layout_height="wrap_content"
android:text="#string/ref2" />
</LinearLayout>
My strings.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Mathematicalopr</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="ref">Enter first number</string>
<string name="add">Add</string>
<string name="sub">Subtract </string>
<string name="times">Multiply</string>
<string name="divide">divide</string>
<string name="ref1">Enter second number</string>
<string name="ref2">Answer is</string>
</resources>
Logcat:
02-08 10:50:43.962: W/Trace(2302): Unexpected value from nativeGetEnabledTags: 0
02-08 10:50:43.972: W/Trace(2302): Unexpected value from nativeGetEnabledTags: 0
02-08 10:50:43.972: W/Trace(2302): Unexpected value from nativeGetEnabledTags: 0
02-08 10:50:44.022: W/Trace(2302): Unexpected value from nativeGetEnabledTags: 0
02-08 10:50:44.022: W/Trace(2302): Unexpected value from nativeGetEnabledTags: 0
02-08 10:50:44.422: D/AndroidRuntime(2302): Shutting down VM
02-08 10:50:44.432: W/dalvikvm(2302): threadid=1: thread exiting with uncaught exception (group=0xb2ca6908)
02-08 10:50:44.462: E/AndroidRuntime(2302): FATAL EXCEPTION: main
02-08 10:50:44.462: E/AndroidRuntime(2302): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathematicalopr/com.example.mathematicalopr.MainActivity}: java.lang.NumberFormatException: Invalid int: "Enter first number"
02-08 10:50:44.462: E/AndroidRuntime(2302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-08 10:50:44.462: E/AndroidRuntime(2302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-08 10:50:44.462: E/AndroidRuntime(2302): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-08 10:50:44.462: E/AndroidRuntime(2302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-08 10:50:44.462: E/AndroidRuntime(2302): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 10:50:44.462: E/AndroidRuntime(2302): at android.os.Looper.loop(Looper.java:137)
02-08 10:50:44.462: E/AndroidRuntime(2302): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-08 10:50:44.462: E/AndroidRuntime(2302): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 10:50:44.462: E/AndroidRuntime(2302): at java.lang.reflect.Method.invoke(Method.java:511)
02-08 10:50:44.462: E/AndroidRuntime(2302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-08 10:50:44.462: E/AndroidRuntime(2302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-08 10:50:44.462: E/AndroidRuntime(2302): at dalvik.system.NativeStart.main(Native Method)
02-08 10:50:44.462: E/AndroidRuntime(2302): Caused by: java.lang.NumberFormatException: Invalid int: "Enter first number"
02-08 10:50:44.462: E/AndroidRuntime(2302): at java.lang.Integer.invalidInt(Integer.java:138)
02-08 10:50:44.462: E/AndroidRuntime(2302): at java.lang.Integer.parse(Integer.java:375)
02-08 10:50:44.462: E/AndroidRuntime(2302): at java.lang.Integer.parseInt(Integer.java:366)
02-08 10:50:44.462: E/AndroidRuntime(2302): at java.lang.Integer.parseInt(Integer.java:332)
02-08 10:50:44.462: E/AndroidRuntime(2302): at com.example.mathematicalopr.MainActivity.onCreate(MainActivity.java:30)
02-08 10:50:44.462: E/AndroidRuntime(2302): at android.app.Activity.performCreate(Activity.java:5104)
02-08 10:50:44.462: E/AndroidRuntime(2302): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-08 10:50:44.462: E/AndroidRuntime(2302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-08 10:50:44.462: E/AndroidRuntime(2302): ... 11 more
Can i know why am i getting such an error? When i try to run this code on a virtual device the application closes unexpectedly. Thank you :)
Thank you everyone. Sorry apparently i don't have enough reputation to thank each one of you personally.
String myFirstNum = display.getText().toString();
final int a = Integer.parseInt(myFirstNum);
String mySecondNum = display1.getText().toString();
final int b = Integer.parseInt(mySecondNum);
insert this code in your add button click event listener then you will get correct data
Your Logcat say it all,
Read this
Invalid int: "Enter first number"
Your are casting a string value to int, which results java.lang.NumberFormatException.
you must change
<EditText
android:id="#+id/tvDisplay"
android:layout_width="342dp"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="#string/ref" />
<EditText
android:id="#+id/tvDisplay1"
android:layout_width="342dp"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="#string/ref1" />
you must change from
android:text="#string/ref1"
to
android:hint="#string/ref1"
because on oncreate() method you are retrieving value of EditText. And in xml you have set string for it and after that you have parsed it in Integer. So you have got invalid int error. So just change it for every EditText.
Your are casting a string value to int, which results java.lang.NumberFormatException.
Modified your code:
package com.example.sampleactivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button add,sub,mul,div;
TextView display2;
EditText display,display1;
int a,b,c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//int c=0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
mul = (Button) findViewById(R.id.times);
div = (Button) findViewById(R.id.divide);
display = (EditText) findViewById(R.id.tvDisplay);
display1 = (EditText) findViewById(R.id.tvDisplay1);
display2 = (TextView) findViewById(R.id.tvDisplay2);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getValues();
c = a+b;
display2.setText("Answer is"+c);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getValues();
c = a-b;
display2.setText("Answer is"+c);
}
});
mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getValues();
c = a*b;
display2.setText("Answer is"+c);
}
});
div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getValues();
c = a/b;
display2.setText("Answer is"+c);
}
});
}
protected void getValues() {
String myFirstNum = display.getText().toString();
a= Integer.parseInt(myFirstNum);
String mySecondNum = display1.getText().toString();
b = Integer.parseInt(mySecondNum);
}
#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;
}
}
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="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/tvDisplay"
android:layout_width="342dp"
android:layout_height="wrap_content"
android:hint="#string/ref"
android:inputType="number" />
<EditText
android:id="#+id/tvDisplay1"
android:layout_width="342dp"
android:layout_height="wrap_content"
android:hint="#string/ref1"
android:inputType="number" />
<Button
android:id="#+id/bAdd"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/add" />
<Button
android:id="#+id/bSub"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/sub" />
<Button
android:id="#+id/times"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/times" />
<Button
android:id="#+id/divide"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/divide" />
<TextView
android:id="#+id/tvDisplay2"
android:layout_width="342dp"
android:layout_height="wrap_content"
android:text="#string/ref2" />
</LinearLayout>

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