My app is crashing. I have errors and I just got done debugging and as the title says thats the problem.
While debugging I found the line that is causing the problem:
listView.setAdapter(arrayAdapter2);
Towards the bottom. I had a similar nullpointerException before here. This time it is different as I am declaring my arrayadapter in the right area. In debugging the result that goes into the array adapter is not NULL.
I have searched the forum and found these:
Why am I getting an InvocationTargetException? Android 2D game
another nullpointerexception
These NullPointerExceptions seem pretty common in android but they are pretty code specific.
In reading the other information correct me if I am wrong the only place the null can be is if the arrayAdapter2 is NULL.
public class ByZipcode extends Activity{
Button btngetLObyzipcode;
Spinner spinner1;
ProgressBar progressBar1;
EditText textinput4byzip;
UserFunctions userFunctions = new UserFunctions();
ArrayAdapter<String> arrayAdapter2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.byzipcodepage);
arrayAdapter2 = new ArrayAdapter<String>(ByZipcode.this,android.R.layout.simple_list_item_1);
// Initializing spinner with predetermined results
spinner1 = (Spinner) findViewById(R.id.spinner1);
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
textinput4byzip = (EditText) findViewById(R.id.textinput4byzip);
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.byzipspinner, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(spinnerAdapter);
btngetLObyzipcode = (Button) findViewById(R.id.btngetLObyzipcode);
btngetLObyzipcode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (textinput4byzip.getText() != null & textinput4byzip.getText().toString().length() == 5 ){
progressBar1.setVisibility(View.VISIBLE);
new DownloadDataTask().execute();
}
}
});
}
#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_main_screen, menu);
return true;
}
private class DownloadDataTask extends AsyncTask<JSONArray, JSONArray, ArrayList<String> > {
#Override
protected ArrayList<String> doInBackground(JSONArray... params) {
String spinValue = spinner1.getSelectedItem().toString();
//if(textinput4byzip.getText() != null)
JSONArray json = userFunctions.getCustomerbyZipCode((textinput4byzip.getText().toString()), spinValue);
ArrayList<String> customers = new ArrayList<String>();
for(int i=0; i < json.length() ; i++) {
JSONObject jarray;
try {
jarray = json.getJSONObject(i);
String zip = jarray.getString("CustomerName");
customers.add(zip);
Log.d(zip,"Output");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return customers;
}
protected void onPostExecute(ArrayList<String> result){
ListView listView = (ListView) findViewById(R.id.listView1);
arrayAdapter2.addAll(result);
listView.setAdapter(arrayAdapter2);
progressBar1.setVisibility(View.GONE);
Intent viewCustomers = new Intent(getApplicationContext(), StoreListView.class);
viewCustomers.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(viewCustomers);
}
}
}
Just in case you are interested the failure is below:
02-10 17:39:36.976: E/AndroidRuntime(7686): FATAL EXCEPTION: main
02-10 17:39:36.976: E/AndroidRuntime(7686): java.lang.NullPointerException
02-10 17:39:36.976: E/AndroidRuntime(7686): at com.example.lo.ByZipcode$DownloadDataTask.onPostExecute(ByZipcode.java:93)
02-10 17:39:36.976: E/AndroidRuntime(7686): at com.example.lo.ByZipcode$DownloadDataTask.onPostExecute(ByZipcode.java:1)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.os.Looper.loop(Looper.java:137)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-10 17:39:36.976: E/AndroidRuntime(7686): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 17:39:36.976: E/AndroidRuntime(7686): at java.lang.reflect.Method.invoke(Method.java:511)
02-10 17:39:36.976: E/AndroidRuntime(7686): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-10 17:39:36.976: E/AndroidRuntime(7686): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-10 17:39:36.976: E/AndroidRuntime(7686): at dalvik.system.NativeStart.main(Native Method)
As requested the xml page for the above activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/textinput4byzip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:contentDescription="#string/zipcodefielddescription"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:inputType="number"
android:maxLength="#integer/zipcodelength"
android:textSize="#dimen/LargeFont" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="#string/entrzip"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="#dimen/MediumFont" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:paddingLeft="#dimen/MediumFont"
android:paddingTop="#dimen/MediumFont" >
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="285dp"
android:layout_height="308dp"
android:layout_gravity="center_horizontal|center_vertical"
android:visibility="gone" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:entries="#array/byzipspinner"
android:textAlignment="center" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/restaurants"
android:textSize="#dimen/LargeFont" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="CheckBox"
android:textSize="#dimen/LargeFont" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="CheckBox"
android:textSize="#dimen/LargeFont" />
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="CheckBox"
android:textSize="#dimen/LargeFont" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:paddingRight="#dimen/MediumFont" >
<Button
android:id="#+id/btngetLObyzipcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/getlo"
android:textSize="#dimen/LargeFont" />
</LinearLayout>
</LinearLayout>
ListView listView = (ListView) findViewById(R.id.listView1);
You're looking in byzipcodepage.xml for a ListView with ID listView1. No such view exists; in fact, there is no ListView in that layout at all.
If you want a list in that view (for the adapter to use), you need to add it; if your ListView is within a different activity, you may need to handle this entire situation a bit differently to pass the data from this activity to that one (perhaps through a Bundle).
Related
I'm developing an app where I have a fragment as my home page. I'm currently trying to call that fragment from an activity but unfortunately it is crashing. I had even implemented the FragmentTransaction and it still doesnt want to pick it up.
logcat
05-02 22:22:40.331 5927-5927/com.example.treycoco.calorietracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.treycoco.calorietracker, PID: 5927
java.lang.NullPointerException: Attempt to invoke virtual method' android.support.v4.app.FragmentTransactio android.support.v4.app.FragmentTransaction.replace(int, android.support.v4.app.Fragment)' on a null object reference at com.example.treycoco.calorietracker.CalorieDetails$1$1.onClick(CalorieDetails.java:79)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
update logcat
example.treycoco.calorietracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.treycoco.calorietracker, PID: 19224
java.lang.IllegalArgumentException: No view found for id 0x7f0e0080 (com.example.treycoco.calorietracker:id/FragmentHolder) for fragment FragmentHome{d567f7e #0 id=0x7f0e0080}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
caloriedetails.java
public class CalorieDetails extends AppCompatActivity {
private TextView foodName, calories, dateTaken;
private Button shareButton;
private int foodId;
private Button deleteButton;
private android.support.v4.app.FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calorie_details);
foodName = (TextView) findViewById(R.id.detsFoodName);
calories = (TextView) findViewById(R.id.detscaloriesValue);
dateTaken = (TextView) findViewById(R.id.detsDateText);
deleteButton = (Button) findViewById(R.id.deleteButton);
Food food = (Food) getIntent().getSerializableExtra("userObj");
foodName.setText(food.getFoodName());
calories.setText(String.valueOf(food.getCalories()));
dateTaken.setText(food.getRecordDate());
foodId = food.getFoodId();
foodName.setTextColor(Color.WHITE);
dateTaken.setTextColor(Color.WHITE);
calories.setTextSize(34.9f);
calories.setTextColor(Color.WHITE);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO: put delete functionality here
android.support.v7.app.AlertDialog.Builder alert = new
android.support.v7.app.AlertDialog.Builder(CalorieDetails.this);
alert.setTitle("Delete?");
alert.setMessage("Are you sure you want to delete this item?");
alert.setNegativeButton("No", null);
alert.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
DatabaseHandler dba = new
DatabaseHandler(getApplicationContext());
dba.deleteFood(foodId);
Toast.makeText(CalorieDetails.this, "Food Item
Deleted!", Toast.LENGTH_SHORT).show();
FragmentHome fragmentHome = new FragmentHome()
fragmentTransaction =
getSupportedFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.FragmentHolder,
fragmentHome);
fragmentTransaction.commit();
CalorieDetails.this.finish();
}
});
alert.show();
}
});
}
}
caloriedetails.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
tools:context=".CalorieDetails"
android:background="#drawable/imgbackground2"
style="#style/AppTheme"
>
<ImageView
android:id="#+id/logo"
android:src="#drawable/weight"
android:layout_centerHorizontal="true"
android:layout_width="180dp"
android:layout_height="180dp" />
<LinearLayout
android:id="#+id/layout"
android:elevation="4dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/logo"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/detsFoodName"
android:elevation="4dp"
android:text="dkdad"
android:textSize="19sp"
android:textStyle="bold"
android:layout_marginTop="18dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/detsCaloriesTitle"
android:text="Calories:"
android:textSize="18sp"
android:layout_marginTop="18dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/detscaloriesValue"
android:text="200"
android:textSize="18sp"
android:layout_marginTop="18dp"
android:layout_gravity="center_horizontal"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/detsDateText"
android:text="Consumed on..."
android:textStyle="italic"
android:textSize="14sp"
android:layout_marginTop="14dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/deleteButton"
android:text="DELETE"
android:textColor="#ffff"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_width="200dp"
android:background="#color/colorBackground2"
android:layout_height="wrap_content" />
</LinearLayout>
You have declared the fragmentManager and fragmentTransaction but not yet initialized.
FragmentHome fragmentHome = new FragmentHome();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.FragmentHolder, fragmentHome);
fragmentTransaction.commit();
Remove this line from onClick method,
CalorieDetails.this.finish();
Because you are going to add the fragment in activity so dont finish the activity.
Add this code in your caloriedetails.xml file
<LinearLayout
android:id="#+id/FragmentHolder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
Please do some google search before start implement.
Your fragmentTransaction is null as you don't initialize it hence the exception.
Do this
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
I have made an application with a listView, the listView contains text and pictures using an array. I alos have an editText where the user can search through the list using a search functioanlity. The listView loads in fine however when I click on the editView to search through my listView I get an outOfMemoryError. I'm not sure if this has something to do with the size of the images or something. I know questions like this have been asked before however none of them fixed my problem.
My Code is as fallows
Insurance.java
public class Insurance extends AppCompatActivity {
ListView list;
ArrayAdapter<String> newadapter;
EditText inputSearch;
String[] itemname ={
"AA",
"Acorn",
"Admiral",
"AIG",
"Allianz",
"Auto Direct",
"Aviva",
"Carrot",
};
Integer[] imgid= {
R.drawable.aa,
R.drawable.acorn,
R.drawable.admiral,
R.drawable.aig,
R.drawable.allianz,
R.drawable.autodirect,
R.drawable.aviva,
R.drawable.carrot,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insurance);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
newadapter = new ArrayAdapter<String>(this, R.layout.mylist, R.id.item, itemname);
inputSearch = (EditText) findViewById(R.id.editText);
list.setAdapter(newadapter);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Insurance.this.newadapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
if (Slecteditem.toString() == "AA") {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:028 9032 2232"));
startActivity(intent);
}
if (Slecteditem.toString() == "Carrot") {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:028 9032 2265"));
startActivity(intent);
}
}
});
Insurance.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"
android:background="#drawable/bg"
tools:context="saveourcar.soc.Insurance">
<EditText
android:layout_width="400dp"
android:layout_height="60dp"
android:id="#+id/editText"
android:layout_marginBottom="50dp"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="107dp">
</ListView>
</RelativeLayout>
myList.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="10"
android:background="#26BFDA">
<LinearLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
>
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="6"
android:orientation="vertical"
>
<TextView
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="20dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:src="#drawable/nextarrow"
/>
</LinearLayout>
</LinearLayout>
I have also included android:largeHeap="true" in my Manifest file.
Logcat
04-15 17:46:49.667 27237-27237/saveourcar.soc E/AndroidRuntime: FATAL EXCEPTION: main
Process: saveourcar.soc, PID: 27237
java.lang.OutOfMemoryError: Failed to allocate a 36000012 byte allocation with 16777216 free bytes and 21MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2453)
at android.content.res.Resources.loadDrawable(Resources.java:2360)
at android.content.res.Resources.getDrawable(Resources.java:768)
at android.content.Context.getDrawable(Context.java:402)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:175)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:168)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:51)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
at saveourcar.soc.CustomListAdapter.getView(CustomListAdapter.java:39)
at android.widget.AbsListView.obtainView(AbsListView.java:2344)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17442)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17442)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1767)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.ru
java.lang.OutOfMemoryError: Failed to allocate a 36000012 byte allocation with 16777216 free bytes and 21MB until OOM
...
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
at saveourcar.soc.CustomListAdapter.getView(CustomListAdapter.java:39)
Your CustomListAdapter is putting a drawable resource in an ImageView. This drawable resource, when decoded and scaled for density, is 36000012 bytes. This is equivalent to a 3000px by 3000px image.
This is much too big. Find out what this resource is and fix it:
Reduce its resolution
If you put it in res/drawable/, move it to a directory that is more indicative of the density you intended that drawable to be used at, and consider having other versions of that drawable, at other resolutions, for other densities
I have also included android:largeHeap="true" in my Manifest file.
Please undo that.
I am creating list view which contain text view,image and google map.
Code for list view 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="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:background="#ffffff"
android:paddingLeft="3dp"
android:paddingRight="3dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:scaleType="fitXY"
android:src="#drawable/profile_pic_bg" />
<TextView
android:id="#+id/textView_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView_time"
android:layout_alignRight="#+id/textView_time"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0A7692"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView_username"
android:layout_toRightOf="#+id/imageView1"
android:gravity="left|center"
android:layout_marginLeft="15dp"
android:layout_marginTop="4dp"
android:textColor="#BBB9BC"
android:text=""
android:textSize="13dp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="3dp"
android:background="#ffffff"
android:paddingRight="3dp" >
<TextView
android:id="#+id/customtxtmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:ellipsize="end"
android:maxLines="5"
android:autoLink="web"
android:linksClickable="true"
android:text=""
android:textColor="#000000" />
<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
android:id="#+id/view_pager"
android:layout_marginLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:background="#ffffff"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:visibility="visible" >
</cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager>
</LinearLayout>
<LinearLayout
android:id="#+id/customwall_lin_setwatchlist"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#eef7fa"
android:gravity="center_vertical" >
<TextView
android:id="#+id/customtxtlike"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#D56438" />
<ImageView
android:id="#+id/customimgdot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:src="#drawable/dot_sep" />
<TextView
android:id="#+id/customtxtcomment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Comment"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#D56438" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|right" >
<TextView
android:id="#+id/customtxttotallikes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:drawableLeft="#drawable/heart_icon"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#D56438" />
<TextView
android:id="#+id/customtxttotalcomments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:drawableLeft="#drawable/comment_icon"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#D56438" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/customlayoutgray"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#a3a8aa"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_temp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#a3a8aa"
android:textSize="6sp" />
</LinearLayout>
</LinearLayout>
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="160dp"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
Code for List view adapter is:-
/********* Adapter class extends with BaseAdapter and implements with OnClickListener ************/
public class CustomAdapter extends BaseAdapter {
/*********** Declare Used Variables *********/
private FragmentActivity activity;
private ArrayList data;
public ArrayList CommentData;
private static LayoutInflater inflater=null;
public Resources res;
CommonWallBean tempValues=null;
ViewHolder holder;
int i=0;
public static String id="";
String TextOfLike="";
SupportMapFragment mapFragment;
GoogleMap map;
HashMap<String, String> mMarkerPlaceLink = new HashMap<String, String>();
Context con;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Context c,FragmentActivity a, ArrayList d,ArrayList CommentData,Resources resLocal) {
/********** Take passed values **********/
activity = a;
data=d;
res = resLocal;
con=c;
CommentData=CommentData;
/*********** Layout inflator to call external xml layout () ***********/
inflater = ( LayoutInflater )activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder{
public TextView customtxtmessage, customtxttime,customtxttotallikes,textView_time,textView_username
,text_likes;
public ViewPager view_pager;
public ImageView user_image,customimguseravtar;
public TextView textViewLike,customtxtcomment,customtxttotalcomments;
public MapView mapView;
}
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final PagerAdapter adapter;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.wall, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
// holder.customtxttime = (TextView) vi.findViewById(R.id.customtxttime);
holder.textView_username=(TextView)vi.findViewById(R.id.textView_username);
holder.customimguseravtar=(ImageView)vi.findViewById(R.id.imageView1);
holder.textView_time=(TextView)vi.findViewById(R.id.textView_time);
holder.view_pager=(ViewPager)vi.findViewById(R.id.view_pager);
holder.textViewLike=(TextView)vi.findViewById(R.id.customtxtlike);
holder.customtxtmessage=(TextView)vi.findViewById(R.id.customtxtmessage);
holder.customtxttotallikes=(TextView)vi.findViewById(R.id.customtxttotallikes);
holder.customtxtcomment=(TextView)vi.findViewById(R.id.customtxtcomment);
holder.customtxttotalcomments=(TextView)vi.findViewById(R.id.customtxttotalcomments);
mapFragment = (SupportMapFragment) activity.getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
map = mapFragment.getMap();
// Getting reference to the SupportMapFragment
// SupportMapFragment fragment = ( SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else{
holder=(ViewHolder)vi.getTag();
}
if(position<data.size()){
/***** Get each Model object from Arraylist ********/
// tempValues=null;
tempValues = ( CommonWallBean ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.textView_time.setText( tempValues.getDate());
holder.textView_username.setText( tempValues.getEmployeeName());
holder.textViewLike.setText(tempValues.getValueOfLike());
holder.customtxtmessage.setText(Html.fromHtml(tempValues.getPostMessage()));
holder.customtxttotallikes.setText(tempValues.getPostLikeCount());
holder.customtxttotalcomments.setText(tempValues.getPostCommentCount());
try {
String url = Const.NewbaseurlPhoto + tempValues.getPhotographFileName();
System.out.println("Thumbnil Url "+url);
Picasso.with(activity).load(url).into(holder.customimguseravtar);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String wallPhotos = tempValues.getWallPhotograph();
String latitude = tempValues.getLatitude();
String longitude = tempValues.getLongitude();
if (latitude != null && !latitude.isEmpty() && !latitude.equals("null")){
holder.view_pager.setVisibility(View.GONE);
String Latitude = tempValues.getLatitude();
String Longitude = tempValues.getLongitude();
//map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Showing / hiding your current location
map.setMyLocationEnabled(false);
// Enable / Disable zooming controls
map.getUiSettings().setZoomControlsEnabled(false);
// Enable / Disable my location button
map.getUiSettings().setMyLocationButtonEnabled(false);
// Enable / Disable Compass icon
map.getUiSettings().setCompassEnabled(false);
// Enable / Disable Rotate gesture
map.getUiSettings().setRotateGesturesEnabled(false);
// Enable / Disable zooming functionality
map.getUiSettings().setZoomGesturesEnabled(false);
MapsInitializer.initialize(con);
// Updates the location and zoom of the MapView
double lat = Double.valueOf("23.012034");
double longi = Double.valueOf("72.510754");
MarkerOptions marker = new MarkerOptions().position(
new LatLng(lat, longi))
.title("Hello Maps");
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
map.addMarker(marker);
//Zoom Particular position
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(lat,
longi)).zoom(12).build();
map.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}else {
holder.view_pager.setVisibility(View.GONE);
}
}
return vi;
}
}
}
}
The Log Cat is :
FATAL EXCEPTION: main
Process: com.vervesys.konnect, PID: 26705
android.view.InflateException: Binary XML file line #178: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
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.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.vervesys.konnect.adapter.CustomAdapter.getView(CustomAdapter.java:130)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.makeAndAddView(ListView.java:1790)
at android.widget.ListView.fillDown(ListView.java:691)
at android.widget.ListView.fillFromTop(ListView.java:752)
at android.widget.ListView.layoutChildren(ListView.java:1630)
at android.widget.AbsListView.onLayout(AbsListView.java:2087)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2013)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
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:5086)
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.IllegalArgumentException: Binary XML file line #178: Duplicate id 0x7f0d00c0, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at android.support.v4.app.BaseFragmentActivityHoneycomb.o
03-18 16:56:08.410 26705-27435/com.vervesys.konnect D/dalvikvm: GC_FOR_ALLOC freed 3139K, 28% free 8554K/11772K, paused 46ms, total 46ms
Please help me to solve this issue, i am not able to figure out what is wrong with this code.
Please help for the same.
Just remove xmlns:android="http://schemas.android.com/apk/res/android"
in this line
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
AND
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
You need have at the end :
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
After struggle a lot for this issue, finally i found that google provides static map in URL.
By using Mapview or fragmentManager will be heavy and app will be slow. so for this better option is to use google static map api.
More details are provided in Android - MapView contained within a Listview and
https://developers.google.com/maps/documentation/staticmaps/
I posted this answer as it may help other.
you can try this line of code in your adapter class for inflating layout
vi = inflater.inflate(R.layout.wall, parent,false);
I get a nullpointer error at smashlist.setAdapter(Lincadapter); in Activity B.
I made sure the id's were all correct so the problem isn't there. What am I doing wrong and how do I fix this?
Activity A -->
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8), (int)(height*.7));
}
public void enter(View view){
Intent i = new Intent(Pop.this, MainActivity.class);
final EditText name = (EditText)findViewById(R.id.name);
final EditText age = (EditText)findViewById(R.id.age);
final EditText ethnicity = (EditText)findViewById(R.id.ethnicity);
final EditText hair = (EditText)findViewById(R.id.hair);
final EditText date = (EditText)findViewById(R.id.date);
final EditText extras = (EditText)findViewById(R.id.extras);
String nameinput = name.getText().toString();
String ageinput = age.getText().toString();
String ethnicityinput = ethnicity.getText().toString();
String hairinput = hair.getText().toString();
String dateinput = date.getText().toString();
String extrasinput = extras.getText().toString();
i.putExtra("name", nameinput);
i.putExtra("age", ageinput);
i.putExtra("ethnicity", ethnicityinput);
i.putExtra("hair", hairinput);
i.putExtra("date", dateinput);
i.putExtra("extras", extrasinput);
startActivity(i);
};
}
Activity A XML -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:maxLines="1"
android:maxLength="25"
android:singleLine="true"
android:capitalize="words"
android:layout_weight="1"
android:id="#+id/name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number"
android:maxLength="2"
android:layout_weight="1"
android:id="#+id/age"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ethnicity"
android:maxLines="1"
android:maxLength="25"
android:singleLine="true"
android:capitalize="words"
android:layout_weight="1"
android:id="#+id/ethnicity"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hair color"
android:maxLines="1"
android:maxLength="25"
android:singleLine="true"
android:capitalize="sentences"
android:layout_weight="1"
android:id="#+id/hair"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:hint="Date"
android:maxLines="1"
android:maxLength="10"
android:singleLine="true"
android:layout_weight="1"
android:id="#+id/date"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Extras"
android:maxLines="3"
android:maxLength="200"
android:capitalize="sentences"
android:layout_weight="1"
android:id="#+id/extras"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/tickbutton"
android:clickable="true"
android:layout_gravity="right"
android:layout_weight="1"
android:onClick="enter"/>
Activity B-->
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Bundle smashdata = getIntent().getExtras();
if (smashdata == null){
return;
}
String nameinput = smashdata.getString("name");
String ageinput = smashdata.getString("age");
String ethnicityinput = smashdata.getString("ethnicity");
String hairinput = smashdata.getString("hair");
String dateinput = smashdata.getString("date");
String extrasinput = smashdata.getString("extras");
final ListView smashlist = (ListView) findViewById(R.id.smashlist);
String[] info = {nameinput, ageinput, ethnicityinput, hairinput, dateinput, extrasinput};
ListAdapter lincadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, info);
smashlist.setAdapter(lincadapter);
smashlist.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
);
Activity B XML -->
<TextView android:text="I've Smashed 0 Girls"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:textColor="#000000"
/>
<ListView
android:id="#+id/smashlist"
android:layout_width="wrap_content"
android:layout_height="272dp"
android:layout_weight="1.00"
android:background="#ff0000"/>
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:clickable="true"
android:src="#drawable/plusbutton"
android:layout_gravity="center_horizontal"
android:onClick="add"/>
Crash log --->
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.kongapps.smashlog.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
I think the error is this:
Activity A
i.putExtra("date", dateinput);
and then
Activity B
String dateinput = smashdata.getString("datename");
you call differently to that variable.
My advice would be to use static constants to be free from such errors . :)
Regards.
it seems your string array has problem, check if the string's inside actually have some value of not other wise you will get Null error
String[] info = {nameinput, ageinput, ethnicityinput, hairinput, dateinput, extrasinput};
for now write your info array like this to verify it works
String[] info = new String[]{"nameinput", "ageinput", "ethnicityinput", "hairinput", "dateinput", "extrasinput"};
Create a static ArrayList in A activity add all values in pass this array in ListView adapter
Example:
ArrayList<String>_array = new ArrayList<>();
_array.add(nameinput);
In B Activity
ListAdapter lincadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, A._array)
Write
String [] info= new String[6];
And then add the values in info={your values};
This might solve your null pointer exception
I'm beginner in android developement. And i have a problem with my code. The application is totally crashed. It is Java error in main.
MainActivity.java:
package hu.cgkni6.uniobuda.testapplication;
import hu.cgkni6.uniobuda.testapplication.R;
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.Toast;
public class MainActivity extends Activity {
final String username = "diak";
final String pass = "diak";
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
okbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (usrname.getText().toString().equals(username))
{
if (passwd.getText().toString().equals(pass))
{
String text2 = "Success!";
Toast.makeText(MainActivity.this, text2, Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this, "Invalid Password", Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(MainActivity.this, "Invalid username", Toast.LENGTH_LONG).show();
}
}
});
cancelbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
#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;
}
}
Activity_main.xml:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#45B7FA" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:text="#string/login_interface" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="#string/username" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="#string/password" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_OK"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:textSize="20sp"
android:text="#string/ok" />
<Button
android:id="#+id/btn_Cancel"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:textSize="20sp"
android:text="#string/cancel" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="3dp" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/about_us"
/>
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/contact_us" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/help" />
</TableRow>
</RelativeLayout>
</LinearLayout>
Catlog:
10-09 09:18:26.244: E/AndroidRuntime(302): FATAL EXCEPTION: main
10-09 09:18:26.244: E/AndroidRuntime(302): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hu.cgkni6.uniobuda.testapplication/hu.cgkni6.uniobuda.testapplication.MainActivity}: java.lang.NullPointerException
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.os.Looper.loop(Looper.java:123)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.reflect.Method.invoke(Method.java:521)
10-09 09:18:26.244: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-09 09:18:26.244: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-09 09:18:26.244: E/AndroidRuntime(302): at dalvik.system.NativeStart.main(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): Caused by: java.lang.NullPointerException
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.Activity.findViewById(Activity.java:1637)
10-09 09:18:26.244: E/AndroidRuntime(302): at hu.cgkni6.uniobuda.testapplication.MainActivity.<init>(MainActivity.java:17)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.Class.newInstanceImpl(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.Class.newInstance(Class.java:1429)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
10-09 09:18:26.244: E/AndroidRuntime(302): ... 11 more
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
Put this in the oncreate method after setContentView(R.layout.activity_main);
and you should be good. You cant do it from that location :)
You should initialize your Views after setting the layout in your onCreate() method.
You are getting the error because you are trying to access the views before setting the layout that is why the application is not able to find the view's id.
Change your code as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
}
You're trying to find views before your layout is setup. Move
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
to your onCreate method. Just after your setContentView call.
First of all you should learn Android life cycle
you should find your all button and edit text in the oncreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
}
Initialize you Button and EditText outside the onCreate method like this
Button okbutton;
Button cancelbutton;
EditText usrname;
EditText passwd;
Then insert this
okbutton = (Button) findViewById(R.id.btn_OK);
cancelbutton = (Button) findViewById(R.id.btn_Cancel);
EditText usrname = (EditText) findViewById(R.id.editText1);
EditText passwd = (EditText) findViewById(R.id.editText2);
inside your onCreate method after the setContentView(R.layout.activity_main);
Hope it helps.