你好我开始在android上创建一个应用程序,它是一个简单的BMI计算器。该应用程序有2个活动(Main带有菜单,BMICalculator
带有小部件。
我的问题是,当我尝试将OnClickListener
我的应用程序崩溃时,为什么要尝试从Main传递给BMIcalculator。我还尝试通过 android:OnClick 来做这个,并且使用这种方法,我可以通过活动,但是当我点击“oblicz”按钮(用于计算BMI)时应用程序崩溃。
我尝试了很多东西,在网上搜索但没有找到任何解决方案:/请帮助我:D 以下是代码:
/ \ OnClickListener如果我不执行getText()
,则工作MainActivity.java
package com.example.labswm;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void gotoP1(View view){
Intent intent = new Intent(this, Page1Activity.class);
startActivity(intent);
}
}
mainXML
<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="com.example.labswm.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/opis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/gotoP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/opis"
android:layout_alignParentBottom="true"
android:layout_marginBottom="132dp"
android:layout_marginLeft="40dp"
android:onClick="gotoP1"
android:text="@string/oblicz_ibm" />
<requestFocus />
</EditText>
BMIACtivity(名为Page1Activity)
package com.example.labswm;
public class Page1Activity extends ActionBarActivity {
public Button oblicz;
public EditText waga;
public EditText wzrost;
public RadioGroup group;
public TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.page1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_page1,
container, false);
return rootView;
}
}
public void calculateClick(View view){
if(view.getId()==R.id.oblicz){
//THE ERROR STARTS HERE !
EditText wag = (EditText)findViewById(R.id.waga);
EditText wzr = (EditText)findViewById(R.id.wzrost);
TextView res = (TextView)findViewById(R.id.result);
float fwaga = Float.parseFloat(wag.getText().toString());
float fwzrost = Float.parseFloat(wzrost.getText().toString());
float rez = (float) (fwaga * 4.88 / fwzrost * fwzrost);
res.setText("BMI" + rez);
}
}
}
Page1Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:launchMode = "singleInstance"
tools:context="com.example.labswm.Page1Activity$PlaceholderFragment" >
<TextView
android:id="@+id/opis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/opis_to_okno_pozwla_na_obliczenie_swojego_ibm_" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/opis"
android:layout_below="@+id/opis"
android:layout_marginTop="32dp"
android:text="@string/waga_" />
<EditText
android:id="@+id/waga"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/waga"
android:layout_below="@+id/waga"
android:text="@string/wzrost_" />
<EditText
android:id="@+id/wzrost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/oblicz"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/group"
android:layout_below="@+id/group"
android:onClick="calculateClick"
android:text="@string/oblicz_" />
<RadioGroup
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/wzrost"
android:layout_below="@+id/wzrost"
android:layout_marginTop="42dp" >
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/centymetry" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/metry"
tools:ignore="ObsoleteLayoutParam" />
</RadioGroup>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/oblicz"
android:layout_marginTop="31dp"
android:layout_toRightOf="@+id/textView2"
android:textSize="20sp" />
LOG
04-16 21:31:31.063: D/dalvikvm(12362): GC_EXTERNAL_ALLOC freed 87K, 47% free 2855K/5379K, external 0K/0K, paused 77ms
04-16 21:31:31.503: D/CLIPBOARD(12362): Hide Clipboard dialog at Starting input: finished by someone else... !
04-16 21:31:33.153: D/dalvikvm(12362): GC_CONCURRENT freed 121K, 46% free 3076K/5639K, external 328K/1281K, paused 2ms+3ms
04-16 21:31:38.093: W/dalvikvm(12362): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-16 21:31:38.103: E/AndroidRuntime(12362): FATAL EXCEPTION: main
04-16 21:31:38.103: E/AndroidRuntime(12362): java.lang.IllegalStateException: Could not execute method of the activity
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.view.View$1.onClick(View.java:2185)
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.view.View.performClick(View.java:2585)
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.view.View$PerformClick.run(View.java:9299)
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.os.Handler.handleCallback(Handler.java:587)
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.os.Looper.loop(Looper.java:130)
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.app.ActivityThread.main(ActivityThread.java:3691)
04-16 21:31:38.103: E/AndroidRuntime(12362): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 21:31:38.103: E/AndroidRuntime(12362): at java.lang.reflect.Method.invoke(Method.java:507)
04-16 21:31:38.103: E/AndroidRuntime(12362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-16 21:31:38.103: E/AndroidRuntime(12362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-16 21:31:38.103: E/AndroidRuntime(12362): at dalvik.system.NativeStart.main(Native Method)
04-16 21:31:38.103: E/AndroidRuntime(12362): Caused by: java.lang.reflect.InvocationTargetException
04-16 21:31:38.103: E/AndroidRuntime(12362): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 21:31:38.103: E/AndroidRuntime(12362): at java.lang.reflect.Method.invoke(Method.java:507)
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.view.View$1.onClick(View.java:2180)
04-16 21:31:38.103: E/AndroidRuntime(12362): ... 11 more
04-16 21:31:38.103: E/AndroidRuntime(12362): Caused by: java.lang.NullPointerException
04-16 21:31:38.103: E/AndroidRuntime(12362): at com.example.labswm.Page1Activity.calculateClick(Page1Activity.java:89)
04-16 21:31:38.103: E/AndroidRuntime(12362): ... 14 more
04-16 21:31:40.113: I/dalvikvm(12362): threadid=4: reacting to signal 3
04-16 21:31:40.113: I/dalvikvm(12362): Wrote stack traces to '/data/anr/traces.txt'
答案 0 :(得分:0)
在此,您声明了EditText
个变量:
public EditText waga;
public EditText wzrost;
但你永远不会初始化它们。
您需要waga = (EditText)findViewById(R.id.waga);
您遇到的具体错误是第89行的空指针,这意味着wzrost
为空,因此.getText()
会导致异常。
答案 1 :(得分:0)
看看这一行:
float fwzrost = Float.parseFloat(wzrost.getText().toString());
您正在使用wzrost
,但未对其进行初始化。您刚刚初始化wzr
==&gt; NPE
答案 2 :(得分:0)
您在wzrost
中使用的是未初始化的变量float fwzrost = Float.parseFloat(wzrost.getText().toString());
。我猜你的意思是wzr
。
答案 3 :(得分:-1)
Button b1=(Button)findViewById(R.id.gotoP1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {}
});
你的xml没问题但是在MainActivity中你必须定义那个按钮