我在Android Studio中创建了一个简单的微波功率计算器应用。我今晚才刚刚下载了此版本,并且以前没有为Android编写任何内容。
当我在手机上运行应用程序时,我的代码显示没有错误,当我点击计算按钮时,它强制关闭,日志中出现以下错误。
01-09 02:33:43.885 12397-12397/com.microwave D/OpenGLRenderer﹕ Enabling debug mode 0
01-09 02:33:48.319 12397-12397/com.microwave W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection
01-09 02:33:48.319 12397-12397/com.microwave W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection
01-09 02:33:48.870 12397-12397/com.microwave W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection
01-09 02:33:48.870 12397-12397/com.microwave W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection
01-09 02:33:50.591 12397-12397/com.microwave W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection
01-09 02:33:50.591 12397-12397/com.microwave W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection
01-09 02:33:51.862 12397-12397/com.microwave W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection
01-09 02:33:51.862 12397-12397/com.microwave W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection
01-09 02:33:53.404 12397-12397/com.microwave W/ResourceType﹕ No known package when getting value for resource number 0xffffff38
01-09 02:33:53.404 12397-12397/com.microwave D/AndroidRuntime﹕ Shutting down VM
01-09 02:33:53.404 12397-12397/com.microwave W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41ed6d40)
01-09 02:33:53.414 12397-12397/com.microwave E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.microwave, PID: 12397
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5034)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1270)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1086)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5034)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1270)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1086)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0xffffff38
at android.content.res.Resources.getText(Resources.java:244)
at android.content.res.XResources.getText(XResources.java:696)
at android.widget.TextView.setText(TextView.java:3888)
at com.microwave.MainActivity.calculate(MainActivity.java:62)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5034)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1270)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1086)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
这是代码的其余部分和XML。我认为我有一些非常基本的错误,因为我只是使用以前的基本Java知识而对android一无所知。
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Recipe Power"
android:id="@+id/textView" />
<EditText
android:layout_width="106dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/recipeP"
android:layout_gravity="right"
android:enabled="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Microwave Power"
android:id="@+id/textView2" />
<EditText
android:layout_width="106dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/microwaveP"
android:layout_gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Recipe Time"
android:id="@+id/textView3" />
<EditText
android:layout_width="106dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/recipeT"
android:layout_gravity="right" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="@+id/calculateButton"
android:layout_gravity="center_horizontal"
android:nestedScrollingEnabled="false"
android:enabled="true"
android:onClick="calculate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/result"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
主要活动
package com.microwave;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
EditText microwaveP;
EditText recipeP;
EditText recipeT;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void calculate(View view){
int recipePower = Integer.parseInt(recipeP.getText().toString());
int microwavePower = Integer.parseInt(microwaveP.getText().toString());
int recipeTime = Integer.parseInt(recipeT.getText().toString());
int difference = recipePower - microwavePower;
int differencePercent = recipePower / difference;
int microwaveTime = recipeTime * differencePercent;
result.setText(microwaveTime);
}
}
答案 0 :(得分:1)
在setContentView之后添加此代码以初始化您的视图:
microwaveP = (EditText)findViewById(R.id.microwaveP);
recipeP = (EditText)findViewById(R.id.recipeP);
recipeT = (EditText)findViewById(R.id.recipeT);
result = (TextView)findViewById(R.id.result);