我是Android的新手,我正在编写一个简单的程序来执行基本计算,例如加,减,除和乘以及使用意图但是当我尝试运行我的程序的一半来检查时,它给出了一个错误:< / p>
这是我的主文件......
package com.example.ruupali;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Calculate extends Activity implements OnClickListener{
Button add,sub,mul,divide;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add=(Button) findViewById(R.id.buttADD);
sub=(Button) findViewById(R.id.buttSUB);
mul=(Button) findViewById(R.id.buttMUL);
divide=(Button) findViewById(R.id.buttDIVIDE);
add.setOnClickListener(this);
sub.setOnClickListener(this);
mul.setOnClickListener(this);
divide.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.buttADD:
Intent intent = new Intent(this, Add.class);
this.startActivity(intent);
break;
case R.id.buttSUB:
Intent intent1 = new Intent(this, Add.class);
this.startActivity(intent1);
break;
case R.id.buttMUL:
Intent intent2= new Intent(this, Add.class);
this.startActivity(intent2);
break;
case R.id.buttDIVIDE:
Intent intent3 = new Intent(this, Add.class);
this.startActivity(intent3);
break;
}
}
}
我的xml文件......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttADD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ADD" />
<Button
android:id="@+id/buttSUB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="SUBTRACT" />
<Button
android:id="@+id/buttMUL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="MULTIPLY"/>
<Button
android:id="@+id/buttDIVIDE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="DIVIDE" />
</LinearLayout>
清单文件......
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ruupali"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.ruupali.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.ruupali.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Email"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Calculate"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
... logcat中
07-29 15:35:23.401: D/dalvikvm(1270): GC_FOR_ALLOC freed 35K, 4% free 3548K/3660K, paused 12ms, total 12ms
07-29 15:35:23.409: I/dalvikvm-heap(1270): Grow heap (frag case) to 4.306MB for 805388-byte allocation
07-29 15:35:23.417: D/dalvikvm(1270): GC_FOR_ALLOC freed <1K, 3% free 4334K/4448K, paused 9ms, total 9ms
07-29 15:35:24.229: E/MediaPlayer(1270): Should have subtitle controller already set
07-29 15:35:24.253: D/libEGL(1270): loaded /system/lib/egl/libEGL_genymotion.so
07-29 15:35:24.253: D/(1270): HostConnection::get() New Host Connection established 0xb96e7ae8, tid 1270
07-29 15:35:24.277: D/libEGL(1270): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
07-29 15:35:24.281: D/libEGL(1270): loaded /system/lib/egl/libGLESv2_genymotion.so
07-29 15:35:24.361: W/EGL_genymotion(1270): eglSurfaceAttrib not implemented
07-29 15:35:24.361: E/OpenGLRenderer(1270): Getting MAX_TEXTURE_SIZE from GradienCache
07-29 15:35:24.365: E/OpenGLRenderer(1270): MAX_TEXTURE_SIZE: 16384
07-29 15:35:24.381: E/OpenGLRenderer(1270): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
07-29 15:35:24.381: E/OpenGLRenderer(1270): MAX_TEXTURE_SIZE: 16384
07-29 15:35:24.381: D/OpenGLRenderer(1270): Enabling debug mode 0
07-29 15:35:29.241: D/dalvikvm(1270): GC_FOR_ALLOC freed 7K, 2% free 4585K/4672K, paused 3ms, total 3ms
07-29 15:35:29.245: I/dalvikvm-heap(1270): Grow heap (frag case) to 6.970MB for 2536932-byte allocation
07-29 15:35:29.253: D/dalvikvm(1270): GC_FOR_ALLOC freed <1K, 2% free 7062K/7152K, paused 6ms, total 6ms
07-29 15:35:29.389: W/EGL_genymotion(1270): eglSurfaceAttrib not implemented
07-29 15:35:34.129: D/AndroidRuntime(1270): Shutting down VM
07-29 15:35:34.129: W/dalvikvm(1270): threadid=1: thread exiting with uncaught exception (group=0xa4d3db20)
07-29 15:35:34.137: E/AndroidRuntime(1270): FATAL EXCEPTION: main
07-29 15:35:34.137: E/AndroidRuntime(1270): Process: com.example.ruupali, PID: 1270
07-29 15:35:34.137: E/AndroidRuntime(1270): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ruupali/com.example.ruupali.Calculate}: java.lang.NullPointerException
07-29 15:35:34.137: E/AndroidRuntime(1270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-29 15:35:34.137: E/AndroidRuntime(1270): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-29 15:35:34.137: E/AndroidRuntime(1270): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-29 15:35:34.137: E/AndroidRuntime(1270): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-29 15:35:34.137: E/AndroidRuntime(1270): at android.os.Handler.dispatchMessage(Handler.java:102)
07-29 15:35:34.137: E/AndroidRuntime(1270): at android.os.Looper.loop(Looper.java:136)
07-29 15:35:34.137: E/AndroidRuntime(1270): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-29 15:35:34.137: E/AndroidRuntime(1270): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 15:35:34.137: E/AndroidRuntime(1270): at java.lang.reflect.Method.invoke(Method.java:515)
07-29 15:35:34.137: E/AndroidRuntime(1270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-29 15:35:34.137: E/AndroidRuntime(1270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-29 15:35:34.137: E/AndroidRuntime(1270): at dalvik.system.NativeStart.main(Native Method)
07-29 15:35:34.137: E/AndroidRuntime(1270): Caused by: java.lang.NullPointerException
07-29 15:35:34.137: E/AndroidRuntime(1270): at com.example.ruupali.Calculate.onCreate(Calculate.java:19)
07-29 15:35:34.137: E/AndroidRuntime(1270): at android.app.Activity.performCreate(Activity.java:5231)
07-29 15:35:34.137: E/AndroidRuntime(1270): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-29 15:35:34.137: E/AndroidRuntime(1270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-29 15:35:34.137: E/AndroidRuntime(1270): ... 11 more
07-29 15:35:36.241: I/Process(1270): Sending signal. PID: 1270 SIG: 9
现在再次检查所有
nw logcat ...
07-29 16:27:44.390: E/MediaPlayer(1778): Should have subtitle controller already set
07-29 16:27:44.506: W/EGL_genymotion(1778): eglSurfaceAttrib not implemented
07-29 16:27:49.466: W/EGL_genymotion(1778): eglSurfaceAttrib not implemented
07-29 16:27:56.582: D/dalvikvm(1778): GC_FOR_ALLOC freed 22K, 3% free 7141K/7352K, paused 3ms, total 3ms
07-29 16:27:56.714: W/EGL_genymotion(1778): eglSurfaceAttrib not implemented
07-29 16:27:58.606: D/AndroidRuntime(1778): Shutting down VM
07-29 16:27:58.606: W/dalvikvm(1778): threadid=1: thread exiting with uncaught exception (group=0xa4d6cb20)
07-29 16:27:58.606: E/AndroidRuntime(1778): FATAL EXCEPTION: main
07-29 16:27:58.606: E/AndroidRuntime(1778): Process: com.example.ruupali, PID: 1778
07-29 16:27:58.606: E/AndroidRuntime(1778): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ruupali/com.example.ruupali.Add}: java.lang.ClassCastException: com.example.ruupali.Add cannot be cast to android.app.Activity
07-29 16:27:58.606: E/AndroidRuntime(1778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-29 16:27:58.606: E/AndroidRuntime(1778): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-29 16:27:58.606: E/AndroidRuntime(1778): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-29 16:27:58.606: E/AndroidRuntime(1778): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-29 16:27:58.606: E/AndroidRuntime(1778): at android.os.Handler.dispatchMessage(Handler.java:102)
07-29 16:27:58.606: E/AndroidRuntime(1778): at android.os.Looper.loop(Looper.java:136)
07-29 16:27:58.606: E/AndroidRuntime(1778): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-29 16:27:58.606: E/AndroidRuntime(1778): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 16:27:58.606: E/AndroidRuntime(1778): at java.lang.reflect.Method.invoke(Method.java:515)
07-29 16:27:58.606: E/AndroidRuntime(1778): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-29 16:27:58.606: E/AndroidRuntime(1778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-29 16:27:58.606: E/AndroidRuntime(1778): at dalvik.system.NativeStart.main(Native Method)
07-29 16:27:58.606: E/AndroidRuntime(1778): Caused by: java.lang.ClassCastException: com.example.ruupali.Add cannot be cast to android.app.Activity
07-29 16:27:58.606: E/AndroidRuntime(1778): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-29 16:27:58.606: E/AndroidRuntime(1778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
07-29 16:27:58.606: E/AndroidRuntime(1778): ... 11 more
07-29 16:28:00.686: I/Process(1778): Sending signal. PID: 1778 SIG: 9
07-29 16:28:00.906: D/dalvikvm(1845): GC_FOR_ALLOC freed 43K, 6% free 3771K/4008K, paused 14ms, total 15ms
07-29 16:28:00.910: I/dalvikvm-heap(1845): Grow heap (frag case) to 6.284MB for 2536932-byte allocation
07-29 16:28:00.950: D/dalvikvm(1845): GC_FOR_ALLOC freed <1K, 4% free 6248K/6488K, paused 40ms, total 40ms
07-29 16:28:01.142: D/libEGL(1845): loaded /system/lib/egl/libEGL_genymotion.so
07-29 16:28:01.142: D/(1845): HostConnection::get() New Host Connection established 0xb901d8e0, tid 1845
07-29 16:28:01.174: D/libEGL(1845): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
07-29 16:28:01.174: D/libEGL(1845): loaded /system/lib/egl/libGLESv2_genymotion.so
07-29 16:28:01.274: W/EGL_genymotion(1845): eglSurfaceAttrib not implemented
07-29 16:28:01.278: E/OpenGLRenderer(1845): Getting MAX_TEXTURE_SIZE from GradienCache
07-29 16:28:01.278: E/OpenGLRenderer(1845): MAX_TEXTURE_SIZE: 16384
07-29 16:28:01.302: E/OpenGLRenderer(1845): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
07-29 16:28:01.306: E/OpenGLRenderer(1845): MAX_TEXTURE_SIZE: 16384
07-29 16:28:01.306: D/OpenGLRenderer(1845): Enabling debug mode 0
答案 0 :(得分:0)
替换
setContentView(R.layout.activity_main);
使用
setContentView(R.layout.calculate);
当你有按钮时
calculate.xml
而不是activity_main.xml
。这就是你获得NPE的原因
<强>更新强>
替换
Intent intent = new Intent(this, Add.class);
this.startActivity(intent);
使用
Intent intent = new Intent(Calculator.this, Add.class);
startActivity(intent);