Java 当我启动apk文件时,出现以下错误
当我运行我的应用程序时,会显示以下错误消息:Java 当我启动apk文件时,出现以下错误,java,android,xml,eclipse,android-layout,Java,Android,Xml,Eclipse,Android Layout,当我运行我的应用程序时,会显示以下错误消息: Unfortunately appName has stopped error 我有以下MainActivity.java文件: package com.shubhammyapp1.myapp1; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.
Unfortunately appName has stopped error
我有以下MainActivity.java文件:
package com.shubhammyapp1.myapp1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener((OnClickListener) this);
}
@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;
}
public void onClick(View arg0) {
EditText et1= (EditText)findViewById(R.id.ocinb);
EditText et2= (EditText)findViewById(R.id.ocint);
TextView tv1= (TextView) findViewById(R.id.textView1);
Double ocb=Double.parseDouble(et1.getText().toString());
Double oct=Double.parseDouble(et2.getText().toString());
Double ocr;
ocr=(ocb-oct)*0.15;
tv1.setText("The %OC is"+ocr);
}
}
/*my activiti_main.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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="37dp"
android:text="1.Organic Carbon" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ocinb"
android:layout_alignRight="@+id/ocinb"
android:layout_marginRight="33dp"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ocinb"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/ocinb"
android:layout_marginLeft="36dp"
android:text="B=" />
<EditText
android:id="@+id/ocinb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/ocint"
android:layout_below="@+id/textView1"
android:layout_marginTop="36dp"
android:layout_toRightOf="@+id/textView6"
android:ems="5"
android:inputType="numberSigned|numberDecimal" />
<EditText
android:id="@+id/ocint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ocinb"
android:layout_marginTop="36dp"
android:layout_toRightOf="@+id/textView6"
android:ems="5"
android:inputType="numberSigned|numberDecimal" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/ocinb"
android:layout_alignBottom="@+id/ocinb"
android:layout_alignLeft="@+id/textView1"
android:layout_marginLeft="15dp"
android:text="B=" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/ocint"
android:layout_alignBottom="@+id/ocint"
android:layout_alignLeft="@+id/textView3"
android:text="T=" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ocint"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:onClick="show"
android:text="Result" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="55dp"
android:layout_toRightOf="@+id/textView1"
android:text="TextView" />
</RelativeLayout>
我看到很多可能会导致错误的事情,所以你应该发布日志,以便我们知道是什么导致了错误。但是,我看到一件事,肯定会导致错误。你有
android:onClick="show"
用于xml中的按钮
。您将获得MethodNotFoundException
,我相信是的,因为您的代码中没有名为show(View)
的方法。如果这样做,则无需通过编程方式设置OnClickListener
删除onClick()
函数和b.setOnClickListener((OnClickListener)this)
并添加一个类似
public void show(View v)
{
// your code for button click here
}
这一点在本手册中有解释
此外,在解析时还应该进行一些错误检查。例如,这些行
Double ocb=Double.parseDouble(et1.getText().toString());
如果您还没有做任何事情,则应该用
try/catch
或进行一些其他错误检查,以防用户输入无效字符,例如字母或什么都没有。我看到很多可能导致错误的事情,所以您应该发布日志,以便我们知道是什么导致了错误。但是,我看到一件事,肯定会导致错误。你有
android:onClick="show"
用于xml中的按钮
。您将获得MethodNotFoundException
,我相信是的,因为您的代码中没有名为show(View)
的方法。如果这样做,则无需通过编程方式设置OnClickListener
删除onClick()
函数和b.setOnClickListener((OnClickListener)this)
并添加一个类似
public void show(View v)
{
// your code for button click here
}
这一点在本手册中有解释
此外,在解析时还应该进行一些错误检查。例如,这些行
Double ocb=Double.parseDouble(et1.getText().toString());
如果您还没有做任何事情,则应该用
try/catch
或进行一些其他错误检查,以防用户输入无效字符,例如字母或什么都没有。我看到很多可能导致错误的事情,所以您应该发布日志,以便我们知道是什么导致了错误。但是,我看到一件事,肯定会导致错误。你有
android:onClick="show"
用于xml中的按钮
。您将获得MethodNotFoundException
,我相信是的,因为您的代码中没有名为show(View)
的方法。如果这样做,则无需通过编程方式设置OnClickListener
删除onClick()
函数和b.setOnClickListener((OnClickListener)this)
并添加一个类似
public void show(View v)
{
// your code for button click here
}
这一点在本手册中有解释
此外,在解析时还应该进行一些错误检查。例如,这些行
Double ocb=Double.parseDouble(et1.getText().toString());
如果您还没有做任何事情,则应该用
try/catch
或进行一些其他错误检查,以防用户输入无效字符,例如字母或什么都没有。我看到很多可能导致错误的事情,所以您应该发布日志,以便我们知道是什么导致了错误。但是,我看到一件事,肯定会导致错误。你有
android:onClick="show"
用于xml中的按钮
。您将获得MethodNotFoundException
,我相信是的,因为您的代码中没有名为show(View)
的方法。如果这样做,则无需通过编程方式设置OnClickListener
删除onClick()
函数和b.setOnClickListener((OnClickListener)this)
并添加一个类似
public void show(View v)
{
// your code for button click here
}
这一点在本手册中有解释
此外,在解析时还应该进行一些错误检查。例如,这些行
Double ocb=Double.parseDouble(et1.getText().toString());
如果您尚未执行某些操作,则应使用
try/catch
或进行其他错误检查,以防用户输入无效字符,如字母或什么都没有。在Android清单文件上,更改活动定义
android:name="com.shubhammyapp1.MainActivity"
android:label="@string/app_name"
到
在Android清单文件上,更改活动定义
android:name="com.shubhammyapp1.MainActivity"
android:label="@string/app_name"
到
在Android清单文件上,更改活动定义
android:name="com.shubhammyapp1.MainActivity"
android:label="@string/app_name"
到
在Android清单文件上,更改活动定义
android:name="com.shubhammyapp1.MainActivity"
android:label="@string/app_name"
到
请注意,logcat显式地告诉您问题:找不到类。每当我遇到这个错误时,它都是由以下两种原因之一引起的:
请注意,logcat显式地告诉您问题:找不到类。每当我遇到这个错误时,它都是由以下两种原因之一引起的:
请注意,logcat显式地告诉您问题:找不到类。每当我遇到这个错误时,它都是由以下两种原因之一引起的:
请注意,logcat显式地告诉您问题:找不到类。每当我遇到这个错误时,它都是由以下两种原因之一引起的:
由于Android和Java软件包,您可以将其更改为
Android:name=“.MainActivity”
。由于Android和Java软件包,您可以将其更改为Android:name=“.MainActivity”
。由于Android和Java软件包,您可以将其更改为Android:name=“.MainActivity”
。由于Android和Java软件包,您也可以将其更改为Android:name=“.MainActivity”
。