gpt4 book ai didi

java - Android 代码中的 Main.this 错误

转载 作者:行者123 更新时间:2023-12-02 06:00:38 26 4
gpt4 key购买 nike

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button PressMe = (Button)findViewById(R.id.btnPressMe);
PressMe.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, PiggyBank.class));
// TODO Auto-generated method stub

}

});
}

我有一个名为 PiggyBank 的项目。 PiggyBank 包含两个屏幕,一个欢迎屏幕和一个输入/输出屏幕。上面的代码适用于我的第一个屏幕,即欢迎屏幕。欢迎屏幕有一个名为“Press Me”的按钮。当用户按下按钮时,他/她将进入输入/输出屏幕。这就是上面的代码应该做的事情。但我有一个错误。 startActivity(new Intent(Main.this, PiggyBank.class)); 行有两个红色下划线,第一个位于 Main.this 下,第二个位于 PiggyBank.class 下。如何修复这两个错误?

PiggyBank.java

import java.text.DecimalFormat;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class PiggyBank2 extends Activity {
String groupChoice;
int NumberOfQuarters, NumberOfDimes, NumberOfNickels, NumberOfPennies;
double TotalQuarters, TotalDimes, TotalNickels, TotalPennies;
double Quarters, Dimes, Nickels,Pennies;
double sumCost;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_piggy_bank2);
final EditText quarters = (EditText) findViewById(R.id.txtQuaters);
final EditText dimes = (EditText) findViewById(R.id.txtDimes);
final EditText nickels = (EditText) findViewById(R.id.txtNickels);
final EditText pennies = (EditText) findViewById(R.id.txtPennies);
final Spinner group = (Spinner) findViewById(R.id.txtGroup);

Button Calculate = (Button) findViewById(R.id.btnCalc);
final TextView result = ((TextView)findViewById(R.id.txtResult));
Calculate.setOnClickListener(new OnClickListener ()
{
public void OnClick(View v)
{
NumberOfQuarters=Integer.parseInt(quarters.getText().toString());
NumberOfDimes=Integer.parseInt(dimes.getText().toString());
NumberOfNickels=Integer.parseInt(nickels.getText().toString());
NumberOfPennies=Integer.parseInt(pennies.getText().toString());


TotalQuarters = Quarters * NumberOfQuarters;
TotalDimes = Dimes * NumberOfDimes;
TotalNickels = Nickels * NumberOfNickels;
TotalPennies = Pennies * NumberOfPennies;

DecimalFormat currency = new DecimalFormat("$###,###.00");

sumCost = TotalQuarters+TotalDimes+TotalNickels+TotalPennies;



groupChoice= group.getSelectedItem().toString();
result.setText("THIS IS YOUR TOTAL " + groupChoice + " COST IS " + currency.format(sumCost));
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}
});



}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.piggy_bank2, menu);
return true;
}

}

Activity_main.xml:

<?xml version="1.0"?>

-<RelativeLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textStyle="bold" android:textSize="25sp" android:textColor="#FF0099" android:text="Welcome to Piggy Bank!" android:layout_centerHorizontal="true" android:layout_alignParentTop="true" android:id="@+id/txtPiggyBank"/>

<ImageView android:layout_height="250dp" android:layout_width="250dp" android:layout_centerHorizontal="true" android:id="@+id/imageView1" android:src="@drawable/piggybank" android:layout_marginTop="16dp" android:layout_below="@+id/txtPiggyBank"/>

<Button android:layout_height="50dp" android:layout_width="100dp" android:textStyle="bold" android:textColor="#FFFFFF" android:text="Press ME" android:layout_centerHorizontal="true" android:id="@+id/btnPressMe" android:layout_marginTop="48dp" android:layout_below="@+id/imageView1" android:background="#FF0099"/>

</RelativeLayout>

Activity_piggy_bank2.xml

<?xml version="1.0"?>

-<RelativeLayout tools:context=".PiggyBank2" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

<EditText android:layout_height="37dp" android:layout_width="200dp" android:textSize="20sp" android:inputType="number" android:hint="@string/Quaters" android:ems="10" android:layout_marginTop="34dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:id="@+id/editText1"/>

<EditText android:layout_height="37dp" android:layout_width="200dp" android:textSize="20sp" android:inputType="number" android:hint="@string/Dimes" android:ems="10" android:layout_marginTop="23dp" android:id="@+id/editText2" android:layout_below="@+id/editText1" android:layout_alignLeft="@+id/editText1"/>

<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20sp" android:layout_marginTop="28dp" android:id="@+id/txtNickels" android:layout_below="@+id/editText2" android:layout_alignLeft="@+id/txtDimes" android:textColor="#0099FF" android:text="Nickels"/>

<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20sp" android:id="@+id/txtQuaters" android:textColor="#00CF00" android:text="Quaters" android:layout_toRightOf="@+id/editText1" android:layout_alignBottom="@+id/editText1" android:layout_alignBaseline="@+id/editText1"/>

<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20sp" android:id="@+id/txtDimes" android:layout_alignLeft="@+id/txtQuaters" android:textColor="#FF66CC" android:text="Dimes" android:layout_alignBottom="@+id/editText2" android:layout_alignBaseline="@+id/editText2"/>

<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20sp" android:layout_marginTop="50dp" android:id="@+id/txtPennies" android:layout_below="@+id/txtNickels" android:layout_alignLeft="@+id/txtNickels" android:textColor="#FF0066" android:text="Pennies"/>

<EditText android:layout_height="37dp" android:layout_width="200dp" android:textSize="20sp" android:inputType="number" android:hint="@string/Pennies" android:ems="10" android:id="@+id/editText4" android:layout_alignLeft="@+id/editText3" android:layout_alignBottom="@+id/txtPennies" android:layout_alignBaseline="@+id/txtPennies"/>


-<EditText android:layout_height="37dp" android:layout_width="200dp" android:textSize="20sp" android:inputType="number" android:hint="@string/Nickels" android:ems="10" android:id="@+id/editText3" android:layout_alignLeft="@+id/editText2" android:layout_alignTop="@+id/txtNickels">

<requestFocus/>

</EditText>

<Spinner android:layout_height="50dp" android:layout_width="100dp" android:id="@+id/txtGroup" android:layout_below="@+id/txtPennies" android:prompt="@string/Title" android:entries="@array/Groups" android:layout_centerHorizontal="true"/>

<Button android:layout_height="50dp" android:layout_width="200dp" android:layout_marginTop="16dp" android:id="@+id/btnCalc" android:layout_below="@+id/txtGroup" android:text="Calculate" android:layout_centerHorizontal="true"/>

<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="21dp" android:id="@+id/txtResult" android:layout_below="@+id/btnCalc" android:layout_centerHorizontal="true"/>

<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/txtResult" android:layout_alignTop="@+id/txtResult" android:layout_centerHorizontal="true"/>

</RelativeLayout>

AndroidManifest.XML:

<?xml version="1.0" encoding="UTF-8"?>

-<manifest android:versionName="1.0" android:versionCode="1" package="com.example.anneholmes1" xmlns:android="http://schemas.android.com/apk/res/android">

<uses-sdk android:targetSdkVersion="19" android:minSdkVersion="19"/>


-<application android:theme="@style/AppTheme" android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:allowBackup="true">


-<activity android:label="@string/app_name" android:name="com.example.anneholmes1.PiggyBank2">


-<intent-filter>

<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

</application>

</manifest>

最佳答案

您错误地使用了 Main.this 而不是 MainActivity.this,因为您的 Activity 名称是 MainActivty 而不是 Main。

所以正确的方法如下,

public class MainActivity extends Activity 
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button PressMe = (Button)findViewById(R.id.btnPressMe);
PressMe.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, PiggyBank.class));
}
});
}

关于java - Android 代码中的 Main.this 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22703348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com