我已完成对简单程序的编码,但当我尝试在模拟器上运行它时,它会停止,我在LogCat中看到以下错误。我的MainActivity.java
和activity_main.xml
也将在下方提供。
07-23 07:43:17.256: D/AndroidRuntime(1259): Shutting down VM
07-23 07:43:17.256: W/dalvikvm(1259): threadid=1: thread exiting with uncaught exception (group=0xb3ad7ba8)
07-23 07:43:17.296: E/AndroidRuntime(1259): FATAL EXCEPTION: main
07-23 07:43:17.296: E/AndroidRuntime(1259): Process: com.example.owncode, PID: 1259
07-23 07:43:17.296: E/AndroidRuntime(1259): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.owncode/com.example.owncode.MainActivity}: java.lang.NullPointerException
07-23 07:43:17.296: E/AndroidRuntime(1259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-23 07:43:17.296: E/AndroidRuntime(1259): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-23 07:43:17.296: E/AndroidRuntime(1259): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-23 07:43:17.296: E/AndroidRuntime(1259): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-23 07:43:17.296: E/AndroidRuntime(1259): at android.os.Handler.dispatchMessage(Handler.java:102)
07-23 07:43:17.296: E/AndroidRuntime(1259): at android.os.Looper.loop(Looper.java:136)
07-23 07:43:17.296: E/AndroidRuntime(1259): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-23 07:43:17.296: E/AndroidRuntime(1259): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 07:43:17.296: E/AndroidRuntime(1259): at java.lang.reflect.Method.invoke(Method.java:515)
07-23 07:43:17.296: E/AndroidRuntime(1259): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-23 07:43:17.296: E/AndroidRuntime(1259): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-23 07:43:17.296: E/AndroidRuntime(1259): at dalvik.system.NativeStart.main(Native Method)
07-23 07:43:17.296: E/AndroidRuntime(1259): Caused by: java.lang.NullPointerException
07-23 07:43:17.296: E/AndroidRuntime(1259): at android.app.Activity.findViewById(Activity.java:1884)
07-23 07:43:17.296: E/AndroidRuntime(1259): at com.example.owncode.MainActivity.<init>(MainActivity.java:11)
07-23 07:43:17.296: E/AndroidRuntime(1259): at java.lang.Class.newInstanceImpl(Native Method)
07-23 07:43:17.296: E/AndroidRuntime(1259): at java.lang.Class.newInstance(Class.java:1208)
07-23 07:43:17.296: E/AndroidRuntime(1259): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-23 07:43:17.296: E/AndroidRuntime(1259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-23 07:43:17.296: E/AndroidRuntime(1259): ... 11 more
07-23 07:43:27.146: I/Process(1259): Sending signal. PID: 1259 SIG: 9
这是我的JAVA代码。
package com.example.owncode;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText calculatorText = (EditText) findViewById(R.id.calculatorText);
@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.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);
}
public void button_one_click(View view){
calculatorText.append("1");
}
public void button_two_click(View view){
calculatorText.append("2");
}
public void button_three_click(View view){
calculatorText.append("3");
}
public void button_four_click(View view){
calculatorText.append("4");
}
public void button_five_click(View view){
calculatorText.append("5");
}
public void button_six_click(View view){
calculatorText.append("6");
}
public void button_seven_click(View view){
calculatorText.append("7");
}
public void button_eight_click(View view){
calculatorText.append("8");
}
public void button_nine_click(View view){
calculatorText.append("9");
}
public void button_zero_click(View view){
calculatorText.append("0");
}
public void button_plus_click(View view){
calculatorText.append("+");
}
public void button_minus_click(View view){
calculatorText.append("-");
}
}
这是我的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:orientation="horizontal" >
<EditText
android:id="@+id/calculatorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="@string/calculatorText_text" />
<Button
android:id="@+id/button_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_minus"
android:layout_alignBottom="@+id/button_minus"
android:layout_alignParentLeft="true"
android:hint="@string/button_plus"
android:onClick="button_plus_click" />
<Button
android:id="@+id/button_zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_plus"
android:layout_alignBottom="@+id/button_plus"
android:layout_toRightOf="@+id/button_plus"
android:hint="@string/button_zero"
android:onClick="button_zero_click" />
<Button
android:id="@+id/button_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="135dp"
android:hint="@string/button_minus"
android:onClick="button_minus_click" />
<Button
android:id="@+id/button_nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_minus"
android:layout_alignLeft="@+id/button_minus"
android:hint="@string/button_nine"
android:onClick="button_nine_click" />
<Button
android:id="@+id/button_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_zero"
android:layout_toLeftOf="@+id/button_minus"
android:hint="@string/button_eight"
android:onClick="button_eight_click" />
<Button
android:id="@+id/button_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_eight"
android:layout_alignBottom="@+id/button_eight"
android:layout_toLeftOf="@+id/button_eight"
android:hint="@string/button_seven"
android:onClick="button_seven_click" />
<Button
android:id="@+id/button_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_nine"
android:layout_toRightOf="@+id/button_eight"
android:hint="@string/button_six"
android:onClick="button_six_click" />
<Button
android:id="@+id/button_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_six"
android:layout_alignBottom="@+id/button_six"
android:layout_toRightOf="@+id/button_seven"
android:hint="@string/button_five"
android:onClick="button_five_click" />
<Button
android:id="@+id/button_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_five"
android:layout_alignBottom="@+id/button_five"
android:layout_toLeftOf="@+id/button_five"
android:hint="@string/button_four"
android:onClick="button_four_click" />
<Button
android:id="@+id/button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_six"
android:layout_toRightOf="@+id/button_five"
android:hint="@string/button_three"
android:onClick="button_three_click" />
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_three"
android:layout_alignBottom="@+id/button_three"
android:layout_toRightOf="@+id/button_four"
android:hint="@string/button_two"
android:onClick="button_two_click" />
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_two"
android:layout_alignBottom="@+id/button_two"
android:layout_toLeftOf="@+id/button_two"
android:hint="@string/button_one"
android:onClick="button_one_click" />
</RelativeLayout>
请帮忙。
答案 0 :(得分:0)
在onCreate()
下方的setContentView()
内移动以下内容:
EditText calculatorText = (EditText) findViewById(R.id.calculatorText);
活动无法确定在哪里找到&#34;在setContentView之前的onCreate之外的id的editText。
答案 1 :(得分:0)
像这样分解您的代码:
public class MainActivity extends Activity {
EditText calculatorText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calculatorText = (EditText) findViewById(R.id.calculatorText);
}
您的代码存在的问题是,您首先尝试在内容视图(R.layout.activity_main)中查找视图,但是您没有在尝试实例化时设置活动的内容视图那个观点!
答案 2 :(得分:0)
在设置布局之前,你正在调用findviewById!
这一行
EditText calculatorText = (EditText) findViewById(R.id.calculatorText);
应该是:
EditText calculatorText;
然后在你之后:
setContentView(R.layout.activity_main);
写道:
calculatorText = (EditText) findViewById(R.id.calculatorText);