我正在为机器人创建一个测验应用。我前两个有3个片段和3个复选框。在最后一个片段上有一个按钮,当按下该按钮时会打开一个名为" End"的活动。我需要在" End"活动结果将显示一个人选择了多少正确答案。
首先,我试图检查它是否只使用了第一个片段,但是我被卡住了。当我按下按钮时应用程序关闭。
End.java :
package bandymas.viewpagerexample;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class End extends ActionBarActivity {
private CheckBox checkBox1, checkBox2, checkBox3;
private Button button;
TextView newresult = (TextView)findViewById(R.id.textView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_end);
getSupportActionBar().hide();
addListenerOnButton();
}
private void addListenerOnButton() {
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
//Run when button is clicked
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("CheckBox1 : ").append(checkBox1.isChecked());
result.append("\nCheckbox2 : ").append(checkBox2.isChecked());
result.append("\nCheckBox3 :").append(checkBox3.isChecked());
newresult.setText("My Awesome Text");
}
});
}
@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_naujas_baigimas, 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);
}
}
这是我的 logcat :
04-01 20:04:37.564 6020-6020/bandymas.viewpagerexample W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-01 20:04:37.564 6020-6020/bandymas.viewpagerexample I/PersonaManager﹕ getPersonaService() name persona_policy
04-01 20:04:37.684 6020-6020/bandymas.viewpagerexample I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: (CL3869936)
OpenGL ES Shader Compiler Version: 17.01.11.SPL
Build Date: 01/17/14 Fri
Local Branch:
Remote Branch:
Local Patches:
Reconstruct Branch:
04-01 20:04:37.734 6020-6020/bandymas.viewpagerexample D/OpenGLRenderer﹕ Enabling debug mode 0
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample I/SELinux﹕ Function: selinux_android_load_priority , priority version is VE=SEPF_GT-I9505_4.4.2_0033
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample E/dalvikvm﹕ >>>>> Normal User
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample E/dalvikvm﹕ >>>>> bandymas.viewpagerexample [ userId:0 | appId:10229 ]
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ Late-enabling CheckJNI
04-01 20:07:31.584 7094-7094/bandymas.viewpagerexample W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-01 20:07:31.594 7094-7094/bandymas.viewpagerexample I/PersonaManager﹕ getPersonaService() name persona_policy
04-01 20:07:31.794 7094-7094/bandymas.viewpagerexample I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: (CL3869936)
OpenGL ES Shader Compiler Version: 17.01.11.SPL
Build Date: 01/17/14 Fri
Local Branch:
Remote Branch:
Local Patches:
Reconstruct Branch:
04-01 20:07:31.844 7094-7094/bandymas.viewpagerexample D/OpenGLRenderer﹕ Enabling debug mode 0
04-01 20:07:42.454 7094-7094/bandymas.viewpagerexample W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ VFY: unable to resolve virtual method 11347: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ VFY: unable to resolve virtual method 11353: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ VFY: unable to resolve virtual method 9041: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ VFY: unable to resolve virtual method 365: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ VFY: unable to resolve virtual method 387: Landroid/content/res/TypedArray;.getType (I)I
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-01 20:07:42.525 7094-7094/bandymas.viewpagerexample D/AndroidRuntime﹕ Shutting down VM
04-01 20:07:42.525 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4187fda0)
04-01 20:07:42.525 7094-7094/bandymas.viewpagerexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: bandymas.viewpagerexample, PID: 7094
java.lang.RuntimeException: Unable to start activity ComponentInfo{bandymas.viewpagerexample/bandymas.viewpagerexample.End}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at bandymas.viewpagerexample.End.addListenerOnButton(End.java:33)
at bandymas.viewpagerexample.End.onCreate(End.java:23)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
activity_end.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: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="bandymas.viewpagerexample.End">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Result"
android:id="@+id/textView1"
android:layout_marginTop="100dp"
android:textSize="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 0 :(得分:0)
请检查activity_end.xml中是否有button(R.id.button)
。
我猜这个按钮的id在activity_end.xml中不存在。