I have the following scenario : on button click dialog should be open in a circular reveal effect.So I have implemented the code.On button click I am facing error NoClassDefFoundError:android.view.ViewAnimatorUtils. Below is my code
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button2);
linearLayout=(LinearLayout) findViewById(R.id.linearLay);
button.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(getApplicationContext());
int x=view.getWidth()/2;
int y=view.getHeight()/2;
int finalRadius=Math.max(x,y)/2;
Animator anim= ViewAnimationUtils.createCircularReveal(linearLayout,x,y,0,finalRadius);
anim.start();
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
}
});
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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.example.dhaval.assignment10a.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_marginLeft="50sp"
android:layout_marginTop="50sp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView"
android:layout_marginTop="16dp"
android:id="#id/linearLay">
</LinearLayout>
</RelativeLayout>
Log
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android.view.ViewAnimationUtils
at com.example.dhaval.assignment10a.MainActivity$1.onClick(MainActivity.java:34)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
ViewAnimationUtils was added in API level 21, you need to set minsdk to 21
What #Diveno says is correct, but if you don't want to set minsdk to 21 you can add this check before instantiating the object:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//now you can use ViewAnimationUtils
}
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 just want to ask you how to add an item from my layout x and y, and how to summarize them all.
My main goal why I posted here is to add all items from different layout in one layout, Shopping List (By the way, I'm currently developing a mobile grocery app)
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3724)
at android.view.View.performClick(View.java:4261)
at android.view.View$PerformClick.run(View.java:17356)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3719)
at android.view.View.performClick(View.java:4261)
at android.view.View$PerformClick.run(View.java:17356)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.admin.mobile_grocery.Baby_Food.ocaddtocart(Baby_Food.java:79)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3719)
at android.view.View.performClick(View.java:4261)
at android.view.View$PerformClick.run(View.java:17356)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Baby Diaper (Java) as Layout X
public class Baby_Diaper extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoCart;
Intent i = new Intent(this, Shopping_List.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baby__diaper);
addtoCart = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_diaper);
}
public void SelectItem (View view) {
boolean checked = ((CheckBox) view) .isChecked();
switch (view.getId())
{
case R.id.pampers:
if(checked)
{selection.add("Pampers");}
else
{
selection.remove ("Pampers");
}
break;
case R.id.huggies:
if(checked)
{selection.add("Huggies");}
else
{
selection.remove ("Huggies");
}
break;
case R.id.johnsons:
if(checked)
{selection.add("Johnsons");}
else
{
selection.remove ("Johnsons");
}
break;
case R.id.supreme:
if(checked)
{selection.add("Supreme");}
else
{
selection.remove ("Supreme");
}
break;
}
}
public void ocaddtocart(View view){
String final_shopping_selection = "";
for (String Selections : selection){
final_shopping_selection = final_shopping_selection + Selections + "\n";
}
final_text.setText(final_shopping_selection);
final_text.setEnabled(true);}
public void ocgtshoppinglist (View view){
Intent x = new Intent(Baby_Diaper.this, Shopping_List.class);
x.putExtra("items", final_text.getText().toString());
startActivity(x);
}
Baby Food (Java) as layout Y
public class Baby_Food extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoCart;
Intent i = new Intent(this, Shopping_List.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addtoCart = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
setContentView(R.layout.activity_baby__food);
}
public void SelectItem (View view) {
boolean checked = ((CheckBox) view) .isChecked();
switch (view.getId())
{
case R.id.coryandgate:
if(checked)
{selection.add("Cory & Gate");}
else
{
selection.remove ("Cory & Gate");
}
break;
case R.id.gerber:
if(checked)
{selection.add("Gerber");}
else
{
selection.remove ("Gerber");
}
break;
case R.id.hipp:
if(checked)
{selection.add("Hipp");}
else
{
selection.remove ("Hipp");
}
break;
}
}
public void ocaddtocart(View view){
String final_shopping_selection = "";
for (String Selections : selection){
final_shopping_selection = final_shopping_selection + Selections + "\n";
}
final_text.setText(final_shopping_selection);
final_text.setEnabled(true);
}
public void ocgtshoppinglist (View view){
Intent x = new Intent(Baby_Food.this, Shopping_List.class);
x.putExtra("items", final_text.getText().toString());
startActivity(x);
}
Shopping List (layout where can view all items)
public class Shopping_List extends ActionBarActivity {
TextView final_result_shopping;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping__list);
final_result_shopping= (TextView)findViewById(R.id.final_result_shopping);
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("items");
final_result_shopping.setText(newString);
}
} else {
newString= (String) savedInstanceState.getSerializable("items");
final_result_shopping.setText(newString);
}
}
Baby Diaper (Layout X) 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="com.example.admin.mobile_grocery.Baby_Diaper"
android:id="#+id/baby_diaper">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pampers"
android:id="#+id/pampers"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp"
android:checked="false"
android:onClick="SelectItem"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/huggies"
android:id="#+id/huggies"
android:layout_below="#+id/pampers"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/johnsons"
android:id="#+id/johnsons"
android:layout_below="#+id/huggies"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
android:inputType="textNoSuggestions"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/supreme"
android:id="#+id/supreme"
android:layout_below="#+id/johnsons"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
android:inputType="textNoSuggestions"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/addtocart"
android:id="#+id/addtocart"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:inputType="textNoSuggestions"
android:onClick="ocaddtocart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello Shoppers!"
android:id="#+id/final_shopping_diaper"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO TO SHOPPING LIST"
android:id="#+id/gt_shopping_list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="ocgtshoppinglist"
/>
Baby Food (layout Y) 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="com.example.admin.mobile_grocery.Baby_Food"
android:id="#+id/baby_food">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cory & Gate"
android:id="#+id/coryandgate"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp"
android:checked="false"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gerber"
android:id="#+id/gerber"
android:layout_below="#+id/coryandgate"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hipp"
android:id="#+id/hipp"
android:layout_below="#+id/gerber"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD TO CART / REMOVE"
android:id="#+id/addtocart"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="ocaddtocart"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello Shoppers!"
android:id="#+id/final_shopping_food"
android:layout_below="#+id/hipp"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO TO SHOPPING LIST"
android:id="#+id/gt_shopping_list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
Shopping List (Receiver layout) 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"
tools:context="com.example.admin.mobile_grocery.Shopping_List"
android:id="#+id/shopping_list_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Shopping List"
android:id="#+id/shopping_list"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Shoppers!"
android:id="#+id/final_result_shopping"
android:layout_marginTop="33dp"
android:layout_below="#+id/reservation_list"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
Hope some pro will lend their knowledge to help me in my problem.
Ok I found it.
Replace
addtoCart = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
setContentView(R.layout.activity_baby__food);
With
setContentView(R.layout.activity_baby__food);
addtoCart = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
I'm new in android and I have a program that gives data from localhost via HTTP and parse it to XML and show it in a list in android.But I can't switch to new page(the page that shows the list).My program has java.lang.NullPointerException exception and I can't found where It gives null.please help me and tell me what should I do.
this is error message:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sara.patientapplication/com.example.sara.patientapplication.ViewAllPatientActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.sara.patientapplication.ViewAllPatientActivity.onCreate(ViewAllPatientActivity.java:45)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)at android.app.ActivityThread.access$600(ActivityThread.java:130)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:4745)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:786)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)at dalvik.system.NativeStart.main(Native Method)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)at android.app.ActivityThread.access$600(ActivityThread.java:130)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:4745)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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
this is my main layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".mainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show All Patients"
android:id="#+id/ShowAllPatients"
android:layout_marginTop="25dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add New Patient"
android:id="#+id/AddNewP"
android:layout_below="#+id/ShowAllPatients"
android:layout_marginTop="10dp"/>
</RelativeLayout>
and this is main Activity.there are a button and when I press it I expect go to a list but at that time exception Occurs.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Button ShowAllPatient=(Button)findViewById(R.id.ShowAllPatients);
Button AddNewPatient=(Button)findViewById(R.id.AddNewP);
ShowAllPatient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(getApplicationContext(),ViewAllPatientActivity.class);
startActivity(i);
}
});
}
this my list layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"></ListView>
</LinearLayout>
and this is my the list activity:
public class ViewAllPatientActivity extends ListActivity {
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser=new XMLParser();
static final String URL="http://localhost/AllPatient.php";
static final String Patient_ID="PatientId";
static final String First_Name="PatientFirstName";
static final String Last_Name="PatientLastName";
//static final String Blood_Type="BloodType";
//static final String Phone_Number="phoneNumber";
//static final String Practitioner_ID="practitionerId";
//static final String Email="Email";
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.all_patient);
// String xml=parser.GetXml(URL);
Document doc=parser.DomElement("http://localhost/AllPatient.php");
NodeList nl = doc.getElementsByTagName(Patient_ID);
for (int i=0;i<nl.getLength();i++){
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(Patient_ID,parser.GetValue(e , Patient_ID));
map.put(First_Name,parser.GetValue(e,First_Name));
map.put(Last_Name,parser.GetValue(e,Last_Name));
// map.put(Blood_Type,parser.GetValue(e,Blood_Type));
// map.put(Phone_Number,parser.GetValue(e,Phone_Number));
// map.put(Email,parser.GetValue(e,Email));
// map.put(Practitioner_ID,parser.GetValue(e,Practitioner_ID));
menuItems.add(map);
}
ListAdapter adapter=new SimpleAdapter(this,menuItems,R.layout.list_item,
new String[]{Patient_ID,First_Name,Last_Name},new int[]{R.id.pid,R.id.fName,R.id.lName});
setListAdapter(adapter);
ListView lv=getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String Patient_id = ((TextView) view.findViewById(R.id.pid)).getText().toString();
String First_name = ((TextView) view.findViewById(R.id.fName)).getText().toString();
String Last_name = ((TextView) view.findViewById(R.id.lName)).getText().toString();
Intent in = new Intent(getApplicationContext(), SingleListPatient.class);
in.putExtra(Patient_ID, Patient_id);
in.putExtra(First_Name, First_name);
in.putExtra(Last_Name, Last_name);
startActivity(in);
}
});
}
and also I have some layout for a single list Item:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"><TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" /><TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" /></LinearLayout>
and
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/firstN"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/lastN"
android:layout_toRightOf="#+id/firstN"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Patient ID: "
android:id="#+id/paId"
android:layout_below="#+id/firstN"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/patientid"
android:layout_toRightOf="#+id/paId"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Blood Type: "
android:id="#+id/bt"
android:layout_below="#+id/paId"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/btype"
android:layout_toRightOf="#+id/bt"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Phone Number: "
android:id="#+id/pn"
android:layout_below="#+id/bt"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/pnum"
android:layout_toRightOf="#+id/pn"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Email: "
android:id="#+id/em"
android:layout_below="#+id/pn"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/mail"
android:layout_toRightOf="#+id/em"/>
</RelativeLayout>
please help me.
thanks.
Change:
Intent i=new Intent(getApplicationContext(),ViewAllPatientActivity.class);
startActivity(i);
To:
Intent i=new Intent( mainActivity.this,ViewAllPatientActivity.class);
startActivity(i);
And check if:
Document doc is null
I am new to android app development( from yesterday only i started to learn android ).
I am getting message in AVD like - unfortunately has stopped ,please help me solve this error, I want learn more in android but sucked by this error.. plz..plz.. help me to sove this.
my MainActivity is
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton mb=(ImageButton) findViewById(R.id.imageButton1);
ImageButton mc=(ImageButton) findViewById(R.id.imageButton2);
ImageButton me=(ImageButton) findViewById(R.id.imageButton3);
ImageButton mf=(ImageButton) findViewById(R.id.imageButton4);
ImageButton mg=(ImageButton) findViewById(R.id.imageButton5);
ImageButton mh=(ImageButton) findViewById(R.id.imageButton6);
mb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent activityChangeIntent= new Intent(MainActivity.this,Second.class);
MainActivity.this.startActivity(activityChangeIntent);
}
});
mc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Second.class ));
}
});
me.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Second.class ));
}
});
mf.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Second.class ));
}
});
mg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Second.class ));
}
});
mh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Second.class ));
}
});
}
}
My MainActivity XML is :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg1"
>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*" >
<TableRow>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:scaleType="fitCenter"
android:background="#drawable/mob1" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:padding="20dp"
android:scaleType="fitCenter"
android:background="#drawable/cam1" />
</TableRow>
<TableRow >
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:scaleType="fitCenter"
android:background="#drawable/comp" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:padding="20dp"
android:scaleType="fitCenter"
android:background="#drawable/watch1" />
</TableRow>
<TableRow >
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:scaleType="fitCenter"
android:background="#drawable/abt1" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:padding="20dp"
android:scaleType="fitCenter"
android:background="#drawable/contact1" />
</TableRow>
</TableLayout>
</LinearLayout>
MY Logcat is :
11-04 05:14:36.971: E/AndroidRuntime(1088): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.somayaji.abhi/com.somayaji.abhi.Second}; have you declared this activity in your AndroidManifest.xml?
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Activity.startActivityForResult(Activity.java:3423)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Activity.startActivityForResult(Activity.java:3384)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Activity.startActivity(Activity.java:3626)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.Activity.startActivity(Activity.java:3594)
11-04 05:14:36.971: E/AndroidRuntime(1088): at com.somayaji.abhi.MainActivity$1.onClick(MainActivity.java:40)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.view.View.performClick(View.java:4424)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.view.View$PerformClick.run(View.java:18383)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.os.Handler.handleCallback(Handler.java:733)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.os.Handler.dispatchMessage(Handler.java:95)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.os.Looper.loop(Looper.java:137)
11-04 05:14:36.971: E/AndroidRuntime(1088): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-04 05:14:36.971: E/AndroidRuntime(1088): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 05:14:36.971: E/AndroidRuntime(1088): at java.lang.reflect.Method.invoke(Method.java:515)
11-04 05:14:36.971: E/AndroidRuntime(1088): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-04 05:14:36.971: E/AndroidRuntime(1088): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-04 05:14:36.971: E/AndroidRuntime(1088): at dalvik.system.NativeStart.main(Native Method)
11-04 05:14:49.691: I/Process(1088): Sending signal. PID: 1088 SIG: 9
my Second activity is
public class Second extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mpage);
}
}
My second activity XML is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/TextView" />
</LinearLayout>
plz.. explain why am getting like this.. tell me the solution
Straight from logcat :
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.somayaji.abhi/com.somayaji.abhi.Second}; have you declared this activity in your AndroidManifest.xml?
You have forgotten to declare your Second activity in your manifest xml file.
Try to read the logcat, messages are often very informative and you'll be able to correct your error faster than post it here.
This is an ActivityNotFound Exception.
include
<activity android:name="com.somayaji.abhi.Second" >
</activity>
inside Application tag of your Manifest file.
Declare the Second activity
<activity android:name="com.somayaji.abhi.Second" >
</activity>
in manifest file
Check the topic Add it to manifest
http://developer.android.com/training/basics/firstapp/starting-activity.html
I have an app that needs to capture a customer's signature and then present it in an ImageView. For proof-of-concept, I've written a small app that has a layout with a GestureOverlayView at the top, then a "Save Signature" button, then an ImageView below. When I press the Save Signature button, it should create an image from the GestureOverlayView and display it in the ImageView. Below is the code/layout and then a list of issues:
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.gesture.GestureOverlayView android:id="#+id/GestureView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFFFFF"
android:fadeEnabled="false"
android:gestureStrokeType="multiple"
android:gestureStrokeWidth="1.5"
android:gestureColor="#000000"
android:fadeOffset="600000"
android:eventsInterceptionEnabled="false"
android:alwaysDrawnWithCache="true">
</android.gesture.GestureOverlayView>
<Button android:id="#+id/SubmitButton"
android:text="Save Signature"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<ImageView android:id="#+id/ImageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFFFFF">
</ImageView>
</LinearLayout>
Code:
public class TestActivity extends Activity {
ImageView iv;
GestureOverlayView gv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView)findViewById(R.id.ImageView);
gv = (GestureOverlayView)findViewById(R.id.GestureView);
Button submitButton = (Button)findViewById(R.id.SubmitButton);
submitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Bitmap b = Bitmap.createBitmap(gv.getDrawingCache());
iv.setImageBitmap(b);
}
});
}
}
I get a Null Pointer exception every time on the createBitmap() method. I believe it is because gv.getDrawingCaches() is returning null. As you can see, drawing cache IS enabled so it should return SOMETHING!
Here's the logcat:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.graphics.Bitmap.createBitmap(Bitmap.java:358)
at com.pgov.barcode.TestActivity$2.onClick(TestActivity.java:42)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Any and all help would be greatly appreciated!
Apparently I answered my own question. Even though I used:
android:alwaysDrawnWithCache="true"
I changed the code to:
gv.setDrawingCacheEnabled(true);
Bitmap b = Bitmap.createBitmap(gv.getDrawingCache());
iv.setImageBitmap(b);
gv.setDrawingCacheEnabled(false);
And it works! Not sure why the XML flag doesn't affect it though...