My project won't open on my device, it say's "Unfortunately,myApp has stopped".
this is my manifest:
this is my activity java :
package bismillah.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Project2Activity extends Activity {
public static int a,b,c;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//coding start here!!
//input data
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
//button proses
Button button1=(Button)findViewById(R.id.Button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Spinner bibit = (Spinner)findViewById(R.id.spinner1);
TextView biaya = (TextView)findViewById(R.id.textView6);
if(bibit.getSelectedItem().toString().equals("Cabai Rp.100")){
a=pekerja*(lama*upah)+(jumlah*100);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Tomat Rp.150")){
a=pekerja*(lama*upah)+(jumlah*150);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Timun Rp.200")){
a=pekerja*(lama*upah)+(jumlah*200);
biaya.setText("Biaya Rp." + a);
}
}
}
}
});
//button reset
Button button2=(Button)findViewById(R.id.Button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView biaya = (TextView)findViewById(R.id.textView6);
EditText pek=(EditText)findViewById(R.id.editText1);
EditText lam=(EditText)findViewById(R.id.editText2);
EditText up=(EditText)findViewById(R.id.editText3);
EditText jum=(EditText)findViewById(R.id.editText4);
pek.setText("");
lam.setText("");
up.setText("");
jum.setText("");
biaya.setText("");
}
});
//button pindah activity
Button button3=(Button)findViewById(R.id.Button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i =new Intent(getApplicationContext(),activity2.class);
startActivity(i);
}
});
}
}
and this is my main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bg"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="#string/hello"
android:textColor="#color/warna"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:entries="#array/list" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textColor="#color/warna2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
I tried your code and I had to delete some stuff from your XML file because it was creating some bugs. It might be related to me but here's your XML I was able to use:
But please do not change your XML file before trying the solution below. If it still doesn't work, you should consider the elements I removed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="hello"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
Next I created your Project2Activity java class and activity2 (pretty much empty since you gave no info about it).
So when trying to run your application you get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application.so/com.example.application.so.Project2Activity}: java.lang.NumberFormatException: For input string: ""
and it is at :
at com.example.application.so.Project2Activity.onCreate(Project2Activity.java:24)
now if we consider this lines 23 and 24:
String value=pek.getText().toString(); // line 23
final int pekerja=Integer.parseInt(value); // line 24
The error is that you are trying to read (line 23) your editText Value at the creation of the activity, at that time, your editText is still blank which means value will be equal to "" (empty string).
What you want to do instead is to put your lines from 22 to 33 (the ones below) in a button click event, I tried putting them in your button 1 click event and your app doesn't crash any more:
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
So to summarize, the problem is that you are extracting empty strings from your editTexts and trying to make integers out of them. Instead try making that all reading and integer extracting process on the click of a button where your really need these inputs. just like get the inputs and the final moment.
You should pay attention to your logcat (error messages), it is quite helpful.
What you can also do is to verify that your editTexts are not empty while trying to extract strings from them and raise exception or Toast if they are empty. Nothing too complicated just something like:
if (value.equals("")):
// editText is empty so do something
You can refer to this link for further understanding of how to know when an editText is empty.
I have a slight problem that I have been stuck on. I have narrowed it down and it seems that my application is crashing on create when i am trying to find a text view. I do not understand why, maybe i have been looking at this to long and dont see the obvious.
Any help would be appreciated.
Thanks.
public class FinalActivity extends Activity {
EditText fullName;
EditText serialNumber;
EditText numberOfTicket;
EditText fullCost;
EditText discount;
EditText totalCostOfTickets;
EditText dinnerYesNo;
EditText numberForDinner;
EditText dinnerCost;
EditText vatTv;
EditText totalCost;
SharedPreferences thePrefs;// Shared preferences variable
SharedPreferences.Editor editor;// New editor to add to shared preferences
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
thePrefs = getSharedPreferences(LoginActivity.FIRSTTIMEPREFS, 0);
editor = thePrefs.edit();
fullName = (EditText)findViewById(R.id.personsNameTv);
/*serialNumber = (EditText)findViewById(R.id.serialNumberTv);
numberOfTicket = (EditText)findViewById(R.id.numberOfTicketTv);
fullCost = (EditText)findViewById(R.id.fullCostBeforeTv);
discount = (EditText)findViewById(R.id.discountAmountTV);
totalCostOfTickets = (EditText)findViewById(R.id.totalTicketCostEt);
dinnerYesNo = (EditText)findViewById(R.id.dinnerAvailedTv);
numberForDinner = (EditText)findViewById(R.id.numberForDinnerTv);
dinnerCost = (EditText)findViewById(R.id.dinnerCostTv);
vatTv = (EditText)findViewById(R.id.vatTv);
totalCost = (EditText)findViewById(R.id.totalCostTv);*/
//fullName.setText (thePrefs.getString(PersonalInfoActivity.FULLNAME, ""));
}
}
<LinearLayout 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:background="#404040"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/mainTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:text="#string/ticketSummary"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/promptTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:text="#string/confirmCorrectDetails"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<ScrollView
android:id="#+id/containerLayout"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:background="#drawable/redborder" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingBottom="30dp"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:paddingTop="15dp" >
<TextView
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/serialNumberTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/personsNameTv"
android:layout_marginTop="10dp"
android:text="#string/serialNumber"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/numberOfTicketTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/serialNumberTv"
android:layout_marginTop="10dp"
android:text="#string/numberOfTickets"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/fullCostBeforeTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberOfTicketTv"
android:layout_marginTop="10dp"
android:text="#string/fullCostBeforeDiscount"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/discountAmountTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/fullCostBeforeTv"
android:layout_marginTop="10dp"
android:text="#string/amountOfDisount"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/totalTicketCostEt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/discountAmountTV"
android:layout_marginTop="10dp"
android:text="#string/totalTicketCost"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/dinnerAvailedTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/totalTicketCostEt"
android:layout_marginTop="10dp"
android:text="#string/dinnerAvailed"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/numberForDinnerTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dinnerAvailedTv"
android:layout_marginTop="10dp"
android:text="#string/numberForDinner"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/dinnerCostTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberForDinnerTv"
android:layout_marginTop="10dp"
android:text="#string/dinnerCost"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/vatTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dinnerCostTv"
android:layout_marginTop="10dp"
android:text="#string/vat"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/totalCostTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vatTv"
android:layout_marginTop="10dp"
android:text="#string/totalCost"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</RelativeLayout>
</ScrollView>
<Button
android:id="#+id/confirmBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/containerLayout"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:background="#drawable/buttonshape"
android:onClick="confirmTicketOptions"
android:text="#string/confirm"
android:textStyle="bold" />
</LinearLayout>
There are two possible things possible. Either you want all Textviews or all EditText.
If you want all Textview then replace all your EditTextin java file with Textview.
Textview fullName;
fullName = (Textview )findViewById(R.id.personsNameTv);
If you want all EditText then replace all Textview in xml with EditText.
<EditText
android:id="#+id/personsNameTv"
.../>
In XML you are using Text-view and in java file you define it Edit-text
<TextView
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />
Replace with
<EditText
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />
I'm trying to set to all of the views that are in the widget a click listener that should open an activity upon a click, but for now seems that its not working. Can someone tell me what am I doing wrong?
Here is my code:
public class AthanWidget extends AppWidgetProvider {
private SharedPreferences prefs;
private boolean PAID = false;
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
prefs = context.getSharedPreferences(context.getPackageName(),
Context.MODE_PRIVATE);
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
String currentDateandTime = sdf.format(new Date());
PAID = prefs.getBoolean(Constants.ITEM_PURCHASED, false);
// TODO CHECK IF IMAGE IS FROM SD OR A RESOURCE IS
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent;
if (!PAID) {
intent = new Intent(context, RemoveAds.class);
} else {
intent = new Intent(context, Main.class);
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(appWidgetId, pendingIntent);
views.setTextViewText(R.id.widget_date_title, currentDateandTime);
views.setTextViewText(R.id.widget_arabic_date_title, arabianDate());
displayPrayerTimes(views);
if (prefs.getInt(Constants.BG_ID_PREFS, 1) < 0) {
Bitmap myBitmap = BitmapFactory.decodeFile(prefs.getString(
Constants.BG_SD_PATH, ""));
views.setImageViewBitmap(R.id.widget_bg, myBitmap);
} else {
views.setImageViewResource(R.id.widget_bg,
prefs.getInt(Constants.BG_ID_PREFS, R.drawable.bg24));
}
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
private void displayPrayerTimes(RemoteViews views) {
if (PAID) {
views.setTextViewText(
R.id.widget_fajr,
prefs.getString(Constants.FARJ_ID + "w", "DISABLED").split(
",")[0]);
views.setTextViewText(R.id.widget_dhurh,
prefs.getString(Constants.DHUHR_ID + "w", "DISABLED")
.split(",")[0]);
views.setTextViewText(
R.id.widget_isha,
prefs.getString(Constants.ISHA_ID + "w", "DISABLED").split(
",")[0]);
views.setTextViewText(
R.id.widget_asr,
prefs.getString(Constants.ASR_ID + "w", "DISABLED").split(
",")[0]);
}
views.setTextViewText(
R.id.widget_shorrok,
prefs.getString(Constants.SHORROK_ID + "w", "DISABLED").split(
",")[0]);
views.setTextViewText(
R.id.widget_maghrib,
prefs.getString(Constants.MAGHRIB_ID + "w", "DISABLED").split(
",")[0]);
}
private String arabianDate() {
Date date = new Date(); // هنا التاريخ الصليبي
Calendar cl = Calendar.getInstance();
cl.setTime(date);
final String[] iMonthNames = { "Muharram", "Safar", "Rabi'ul Awwal",
"Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira", "Rajab",
"Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja" };
Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
LocalDate todayIso = new LocalDate(cl.get(Calendar.YEAR),
cl.get(Calendar.MONTH) + 1, cl.get(Calendar.DAY_OF_MONTH), iso);
LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(),
hijri);
return todayHijri.getDayOfMonth() + " "
+ iMonthNames[todayHijri.getMonthOfYear()] + " "
+ todayHijri.getYear();
}
}
and my widget layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/widget_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha=".85"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/widget_toptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:text="#string/widget_title"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/widget_date_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/widget_toptext"
android:layout_centerHorizontal="true"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/widget_arabic_date_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/widget_date_title"
android:layout_centerHorizontal="true"
android:textColor="#android:color/white"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#80000000"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/w_pray_sun_up"
android:drawableRight="#drawable/w_pray_sun_up"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="#string/ar_fajr"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:id="#+id/widget_fajr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/voir"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fajr"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/white" />
<!-- delete this -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/w_pray_sun_up"
android:drawableRight="#drawable/w_pray_sun_up"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="#string/ar_shorok"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:id="#+id/widget_shorrok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/voir"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/shorrok"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/white" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/w_pray_dhurh"
android:drawableRight="#drawable/w_pray_dhurh"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="#string/ar_dhuhr"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:id="#+id/widget_dhurh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/voir"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dhuhr"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/white" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/w_pray_asr"
android:drawableRight="#drawable/w_pray_asr"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="#string/ar_asr"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:id="#+id/widget_asr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/voir"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/asr"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/white" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/w_pray_maghreb"
android:drawableRight="#drawable/w_pray_maghreb"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="#string/ar_magrib"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:id="#+id/widget_maghrib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/voir"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/maghrib"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/white" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/w_pray_moon"
android:drawableRight="#drawable/w_pray_moon"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="#string/ar_isha"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:id="#+id/widget_isha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/voir"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/isha_a"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
<!-- end of deletion -->
</LinearLayout>
</RelativeLayout>
here you are setting click pending intent for the appwidgetId, but that should be view id.
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(appWidgetId, pendingIntent);
like:
if the id of a Button is R.id.button1. which is in widget layout if you want to set click listener to that button. then that should be like this :
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
In the onCreate() method I am updating 2 TextViews to be blank. For some reason it is setting two other TextViews to be blank. I have followed my logic and can not see what is going wrong.
here is my onCreate() method.
import android.widget.TextView;
public class MainActivity extends Activity {
private int purpleDrinks; //number of purple drinks
private int greenDrinks; //number of green drinks
private int redDrinks; //number of red drinks
private int blueDrinks; //number of blue drinks
private TextView purpleCount; //number of purple drinks display
private TextView greenCount; //number of green drinks display
private TextView redCount; //number of red drinks display
private TextView blueCount; //number of blue drinks display
private TextView purchaseConfirm; //displays what the user bought
private TextView outOfStock; //lets the user know when the item is out of stock.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( savedInstanceState == null ) // the app just started running
{
purpleDrinks = 10;
greenDrinks = 10;
redDrinks = 10;
blueDrinks = 10;
} // end if
purpleCount = (TextView) findViewById(R.id.purpleCount);
greenCount = (TextView) findViewById(R.id.greenCount);
redCount = (TextView) findViewById(R.id.redCount);
blueCount = (TextView) findViewById(R.id.blueCount);
purchaseConfirm = (TextView) findViewById(R.id.purchaseConfirm);
outOfStock = (TextView) findViewById(R.id.outOfStock);
purchaseConfirm.setText("");
outOfStock.setText("");
purpleCount.setText(purpleDrinks + "");
greenCount.setText(greenDrinks + "");
redCount.setText(redDrinks + "");
blueCount.setText(blueDrinks + "");
}
here is the xml code: the ids are correct.
<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= "#D5E3EA"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/greenCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:layout_marginTop="26dp"
android:src="#drawable/greencan" />
<ImageButton
android:id="#+id/redCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/greenCann"
android:layout_marginRight="25dp"
android:src="#drawable/redcan" />
<ImageButton
android:id="#+id/purpleCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/redCann"
android:layout_marginRight="25dp"
android:src="#drawable/purplecan" />
<ImageButton
android:id="#+id/blueCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/purpleCann"
android:layout_marginRight="25dp"
android:src="#drawable/bluecan" />
<Button
android:id="#+id/restockButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="54dp"
android:text="#string/restock" />
<TextView
android:id="#+id/purpleCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/purpleCann"
android:layout_marginBottom="18dp"
android:layout_toLeftOf="#+id/purpleCann"
android:text="TextView" />
<TextView
android:id="#+id/redCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/redCann"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="#+id/redCann"
android:text="TextView" />
<TextView
android:id="#+id/greenCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/greenCann"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="#+id/greenCann"
android:text="TextView" />
<TextView
android:id="#+id/blueCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/blueCann"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="#+id/blueCann"
android:text="TextView" />
<TextView
android:id="#+id/outOfStock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/restockButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="TextView" />
<TextView
android:id="#+id/purchaseConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/outOfStock"
android:layout_alignBottom="#+id/outOfStock"
android:layout_alignLeft="#+id/outOfStock"
android:text="TextView" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/greenCount"
android:layout_toLeftOf="#+id/restockButton"
android:text="#string/greenSoda2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/redCount"
android:layout_alignBottom="#+id/redCount"
android:layout_alignLeft="#+id/textView5"
android:text="#string/redSoda"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/purpleCount"
android:layout_alignBottom="#+id/purpleCount"
android:layout_alignLeft="#+id/textView6"
android:text="#string/purpleSoda"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/blueCount"
android:layout_alignBottom="#+id/blueCount"
android:layout_alignLeft="#+id/textView7"
android:text="#string/blueSoda"
android:textAppearance="?android:attr/textAppearanceMedium" />
here is a screenshot of the app:
does anybody know what is going on?
Hi there is my add_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".AddActivity" >
<TextView
android:id="#+id/textViewNome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:text="Nome (univoco):" />
<TextView
android:id="#+id/TextViewURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textViewNome"
android:layout_below="#+id/textViewNome"
android:layout_marginTop="15dp"
android:text="URL:" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/TextViewURL"
android:layout_below="#+id/TextViewURL"
android:layout_marginTop="20dp"
android:text="Aggiorna ogni" />
<EditText
android:id="#+id/editText_URL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/TextViewURL"
android:layout_alignBottom="#+id/TextViewURL"
android:layout_alignRight="#+id/editText_Name"
android:layout_marginLeft="14dp"
android:layout_toRightOf="#+id/TextViewURL"
android:ems="10"
android:inputType="textUri" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textViewNome"
android:layout_alignBottom="#+id/textViewNome"
android:layout_marginLeft="14dp"
android:layout_toRightOf="#+id/textViewNome"
android:ems="10" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/TextView02"
android:layout_alignRight="#+id/editText_URL"
android:layout_marginRight="32dp"
android:text="minuti" />
<TextView
android:id="#+id/textViewErrors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<EditText
android:id="#+id/editText_Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/TextView02"
android:layout_alignBottom="#+id/TextView02"
android:layout_toLeftOf="#+id/textView2"
android:layout_toRightOf="#+id/TextView02"
android:ems="10"
android:inputType="number" />
<ImageButton
android:id="#+id/OkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText_Time"
android:layout_marginTop="28dp"
android:layout_toLeftOf="#+id/CancelButton"
android:background="#null"
android:onClick="OkButton_Handler"
android:src="#drawable/add_icon" />
<ImageButton
android:id="#+id/CancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/OkButton"
android:background="#null"
android:onClick="CancelButton_Handler"
android:src="#drawable/delete_icon" />
And there is my method try to find view of editText_Time:
#Override
protected void onCreate(Bundle savedInstanceState) { //parte quando viene create l'interfaccia
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
Intent intent = getIntent();
if(intent.getExtras().getInt("requestCode") == 1){ //Richiesta modifica
((EditText) findViewById(R.id.editText_Time)).setText(intent.getExtras().getInt("tempo"));
((EditText) findViewById(R.id.editText_Name)).setText(intent.getExtras().getString("nome"));
//((EditText) findViewById(R.id.editText_URL)).setText(intent.getExtras().getString("url"));
id_to_edit = intent.getExtras().getInt("id");
}
}
When launch my application the logcat says Resources$NotFoundException
I really don't understand why he can find editText_Time
Can someone help me? :D
The problem is this line:
((EditText) findViewById(R.id.editText_Time))
.setText(intent.getExtras().getInt("tempo"));
getInt returns an int, so the method setText(int resId) will be called and will search the resource with the value returned by getInt("tempo") which not exists and hence the ResourceNotFoundException.
I assume that you want to set the text with the integer returned by getInt (i.e call the method setText(CharSequence text)). So do:
((EditText) findViewById(R.id.editText_Time))
.setText(String.valueOf(intent.getExtras().getInt("tempo")));
Also don't call getExtras() multiple times. Call it once:
Bundle b = getIntent().getExtras();
if(b.getInt("requestCode") == 1){ //Richiesta modifica
((EditText) findViewById(R.id.editText_Time)).setText(String.valueOf(b.getInt("tempo")));
((EditText) findViewById(R.id.editText_Name)).setText(b.getString("nome"));
//((EditText) findViewById(R.id.editText_URL)).setText(b.getString("url"));
id_to_edit = b.getInt("id");
}