我想从onCreate调用一个方法,但是我的logCat中一直出现错误。我正在查看其他一些答案,但没有人特别回答我的问题。我对android很新,所以我不太了解 这是XML:
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/AbsoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/usd_tot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="116dp"
android:layout_y="120dp"
android:text="Total USD"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/cad_tot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="116dp"
android:layout_y="192dp"
android:text="Total CAD"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/aud_tot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="116dp"
android:layout_y="258dp"
android:text="Total AUD"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/eur_tot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="116dp"
android:layout_y="328dp"
android:text="Total EUR"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/gbp_tot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="116dp"
android:layout_y="408dp"
android:text="Total GBP"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="32dp"
android:layout_y="34dp"
android:text="Start Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="82dp"
android:layout_y="0dp"
android:text="Claim Expenses"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="219dp"
android:layout_y="39dp"
android:text="End Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tot_usd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="118dp"
android:layout_y="150dp"
android:text="TextView" />
<TextView
android:id="@+id/tot_cad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="118dp"
android:layout_y="224dp"
android:text="TextView" />
<TextView
android:id="@+id/tot_aud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="118dp"
android:layout_y="294dp"
android:text="TextView" />
<TextView
android:id="@+id/tot_eur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="118dp"
android:layout_y="370dp"
android:text="TextView" />
<TextView
android:id="@+id/tot_gbp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="118dp"
android:layout_y="435dp"
android:text="TextView" />
<TextView
android:id="@+id/sdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="33dp"
android:layout_y="62dp"
android:text="TextView" />
<TextView
android:id="@+id/edate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="218dp"
android:layout_y="65dp"
android:text="TextView" />
</AbsoluteLayout>
这是代码。
package app.zioueche_travelexpense;
import java.util.ArrayList;
import java.util.Collection;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class GetDetails extends Activity {
static int usd = 0;
static int cad = 0;
static int aud = 0;
static int gbp = 0;
static int eur = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_view);
calculatePrices(); //I WANT TO CALL THIS> DONT KNOW HOW.
}
public ArrayList<Expense> getExpenses(View v){
int finalPosition = getIntent().getIntExtra("claim_position",0);
Toast.makeText(this, finalPosition+"",Toast.LENGTH_SHORT).show();
ClaimListController ct = new ClaimListController();
Collection<Claim> cl = ClaimListController.getClaimList().getClaim();
ArrayList<Claim> clist = new ArrayList<Claim>(cl);
Claim claim = clist.get(finalPosition);
ArrayList<Expense> elist = claim.getExpenses();
return elist;
}
public void calculatePrices(View v){
ArrayList<Expense> list = getExpenses(v);
for (int i = 0; i < list.size(); i++){
Expense e = list.get(i);
if (e.getCurrency().equals("USD") ){
GetDetails.usd += e.getPrice();
}
if (e.getCurrency().equals("CAD")){
GetDetails.cad += e.getPrice();
}
if (e.getCurrency().equals("AUD")){
GetDetails.aud += e.getPrice();
}
if (e.getCurrency().equals("EUR")){
GetDetails.eur += e.getPrice();
}
if (e.getCurrency().equals("GBP")){
GetDetails.gbp += e.getPrice();
}
}
TextView usds = (TextView) findViewById(R.id.tot_usd);
usds.setText(this.usd);
TextView cads = (TextView) findViewById(R.id.tot_cad);
cads.setText(this.cad);
TextView auds = (TextView) findViewById(R.id.tot_aud);
auds.setText(this.aud);
TextView eurs = (TextView) findViewById(R.id.tot_eur);
eurs.setText(this.eur);
TextView gbps = (TextView) findViewById(R.id.tot_gbp);
gbps.setText(this.gbp);
}
}
我评论了我试图调用该方法的行。我知道我需要传递一个VIEW参数,但我不知道如何。我试过View v,this.view,除非我做错了,否则我不知道我做错了什么。如果有人能帮助我,那将是非常棒的。我也会发布logcat。
01-27 18:00:32.021: E/AndroidRuntime(14399): FATAL EXCEPTION: main
01-27 18:00:32.021: E/AndroidRuntime(14399): Process: app.zioueche_travelexpense, PID: 14399
01-27 18:00:32.021: E/AndroidRuntime(14399): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.zioueche_travelexpense/app.zioueche_travelexpense.GetDetails}: android.content.res.Resources$NotFoundException: String resource ID #0x0
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412)
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.app.ActivityThread.access$900(ActivityThread.java:174)
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.os.Handler.dispatchMessage(Handler.java:102)
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.os.Looper.loop(Looper.java:146)
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.app.ActivityThread.main(ActivityThread.java:5593)
01-27 18:00:32.021: E/AndroidRuntime(14399): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 18:00:32.021: E/AndroidRuntime(14399): at java.lang.reflect.Method.invoke(Method.java:515)
01-27 18:00:32.021: E/AndroidRuntime(14399): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
01-27 18:00:32.021: E/AndroidRuntime(14399): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
01-27 18:00:32.021: E/AndroidRuntime(14399): at dalvik.system.NativeStart.main(Native Method)
01-27 18:00:32.021: E/AndroidRuntime(14399): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.content.res.Resources.getText(Resources.java:1431)
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.widget.TextView.setText(TextView.java:4954)
01-27 18:00:32.021: E/AndroidRuntime(14399): at app.zioueche_travelexpense.GetDetails.calculatePrices(GetDetails.java:58)
01-27 18:00:32.021: E/AndroidRuntime(14399): at app.zioueche_travelexpense.GetDetails.onCreate(GetDetails.java:22)
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.app.Activity.performCreate(Activity.java:5458)
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
01-27 18:00:32.021: E/AndroidRuntime(14399): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
01-27 18:00:32.021: E/AndroidRuntime(14399): ... 11 more
答案 0 :(得分:1)
你设法调用它,因为崩溃是在那个方法中。
清理项目,检查任何XML文件中是否存在无错误,以便可以成功生成R
类,然后运行项目。问题出在TextView
资源中。
编辑:您正在尝试将int设置为TextView
,而您无法做到。在您设置值的每一行中使用String.valueOf
。
usds.setText(String.valueOf(this.usd));
OR
usds.setText("" + this.usd);