出于某种原因,我在运行应用程序时遇到Null指针异常。它工作正常,直到它达到我的“ExamActivity”活动。我刚刚添加的代码应该改变背景(根据称为“性别”的可变性)和从SharedPreferences中提取的文本(根据称为“stepnumber”的变量)。
我搜索了互联网,似乎无法找到我的代码出错的地方!这是ExamActivity.java文件:
public class ExamActivity extends ActionBarActivity
{
public void goToNext(View view)
{
//set up shared preferences.
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor;
prefsEditor = myPrefs.edit();
//get the current stepnumber
int stepnumber = myPrefs.getInt("stepnumber", 1);
//increment the step number by 1
stepnumber= stepnumber+1;
//set the value in shared preferences to the new stepnumber.
prefsEditor.putInt("stepnumber", stepnumber );
//commit the preferences
prefsEditor.commit();
//get whether to move to a camera or a stethoscope view.
Boolean camera =myPrefs.getBoolean("camera", true);
if(camera)
{
//go to the new activity.
Intent intent = new Intent(this, CameraActivity.class );
startActivity(intent);
}
else
{
//go to the new activity.
Intent intent = new Intent(this, StethActivity.class );
startActivity(intent);
}
}
public void goBack(View view)
{
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor;
prefsEditor = myPrefs.edit();
//get the step number
int StepNumber= myPrefs.getInt("stepnumber", 1);
//if we are on the first step
if(StepNumber==1)
{
//then go back to the exam start activity!
Intent intent = new Intent(this, ExamStartActivity.class );
startActivity(intent);
}
else
{
//otherwise, decrease the step number!
StepNumber=StepNumber-1;
//change the value stored in the shared preferences
prefsEditor.putInt("stepnumber", StepNumber);
//commit the defaults
prefsEditor.commit();
//go to the new activity.
Intent intent = new Intent(this, ExamActivity.class );
startActivity(intent);
}
}
public void SetVariables( Boolean cam , String title , String filename , String audiofile)
{
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor;
prefsEditor = myPrefs.edit();
//set the top label for the step.
TextView t =new TextView(this);
t=(TextView)findViewById(R.id.textViewExam);
t.setText(title);
//set whether its a camera or stethoscope test.
prefsEditor.putBoolean("camera", cam);
//set the name of the file the result will be saved to
prefsEditor.putString("outputfilename", filename);
//set the audio file
prefsEditor.putString("soundfile", audiofile);
//commit the defaults
prefsEditor.commit();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exam);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
//////////////////BEGIN CUSTOM IMPLEMENTATION////////////////////////////////////////////////
SharedPreferences preferences = getSharedPreferences("myPrefs", MODE_PRIVATE);
//get the selected gender
boolean gender =preferences.getBoolean("gender", false);
//get the step number.
int StepNumber= preferences.getInt("stepnumber", 1);
//if it is female
if(gender)
{
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayoutid);
rl.setBackgroundResource(R.drawable.womanbasefront2);
}
//check which step in the exam we are on.
if(StepNumber == 1)
{
SetVariables(true,"General Head To Toe Pan", "GeneralExam.mov","generalexamaudio");
}
else if(StepNumber==2)
{
SetVariables(true, "Examine Oral Mucosa" ,"OralMucosa.mov","oralmucosaexamaudio");
}
else if(StepNumber ==3 )
{
SetVariables(false ,"Auscultate Aortic Site At Right Upper Sternal Border" ,"AorticCardiacAuscultation.wav","aorticcardiacauscultationexamaudio");
}
else if(StepNumber==4)
{
SetVariables(false ,"Auscultate Pulmonic Site at Left Upper Sternal Border" ,"PulmonicCardiacAuscultation.wav","pulmoniccardiacauscultationexamaudio");
}
else if(StepNumber==5)
{
SetVariables(false,"Auscultate Tricuspid Site At Left Lower Sternal Border" ,"TricuspidCardiacAuscultation.wav","tricuspidcardiacauscultationexamaudio");
}
else if(StepNumber==6)
{
SetVariables(false, "Auscultate Mitral Site at the Apex" ,"MitralCardiacAuscultation.wav","mitralmardiacauscultationexamaudio");
}
else if(StepNumber==7)
{
//flip step
SetVariables(false,"Austcultate Pulmonary Left Apex with deep breaths" ,"LeftApexPulmonaryAuscultation.wav","leftapexpulmonaryauscultationexamaudio");
}
else if(StepNumber==8)
{
//flip step
SetVariables(false,"Auscultate Pulmonary Right Apex with deep breaths","RightApexPulmonaryAuscultation.wav","rightapexpulmonaryauscultationexamaudio");
}
else if(StepNumber==9)
{
//flip step
SetVariables(false, "Auscultate Pulmonary Left Middle with deep breaths" ,"LeftMiddlePulmonaryAuscultation.wav" ,"leftmiddlepulmonaryauscultationexamaudio");
}
else if(StepNumber==10)
{
//flip step
SetVariables(false, "Auscultate Pulmonary Right Middle with deep breaths" ,"RightMiddlePulmonaryAuscultation.wav" ,"rightmiddlepulmonaryauscultationexamaudio");
}
else if(StepNumber==11)
{
//flip step
SetVariables(false, "Auscultate Pulmonary Left Base with deep breaths" ,"LeftBasePulmonaryAuscultation.wav" ,"leftbasepulmonaryauscultationexamaudio");
}
else if(StepNumber==12)
{
//flip step
SetVariables(false ,"Auscultate Pulmonary Right Base with deep breaths" ,"RightBasePulmonaryAuscultation.wav" ,"rightbasepulmonaryauscultationexamaudio");
}
else if(StepNumber==13)
{
//NOT SURE IF CORRECT AUDIO FILE!!!!
SetVariables(true ,"Record Right Jugular Vein from Anterior Mid-Clavicle" ,"RightAnteriorInternalJugularExam.mov", "rightanteriorinternaljugularexamaudio");
}
else if(StepNumber==14)
{
SetVariables(true ,"Record Right Jugular Vein from Lateral Shoulder" ,"RightLateralInternalJugularExam.mov","rightlateralinternaljugularexamaudio");
}
else if(StepNumber==15)
{
SetVariables(true ,"Record Left Jugular Vein from Anterior Mid-Clavicle" ,"LeftAnteriorInternalJugularExam.mov","leftanteriorinternaljugularexamaudio");
}
else if(StepNumber==16)
{
SetVariables(true ,"Record Left Jugular Vein from Lateral Shoulder" ,"LeftLateralInternalJugularExam.mov","leftlateralinternaljugularexamaudio");
}
else if(StepNumber==17)
{
SetVariables(true,"Record right lower extremity from knee after firmly pressing with finger","RightSuperiorLowerExtremityExam.mov","rightsuperiorlowerextremityexamaudio");
}
else if(StepNumber==18)
{
SetVariables(true, "Record right shin 2 feet Anterior to Shin after pressing firmly with finger","RightAnteriorLowerExtremityExam.mov","rightanteriorlowerextremityexamaudio");
}
else if(StepNumber==19)
{
SetVariables(true ,"Record Left Lower Knee Extremity from Knee after pressing lightly with finger","LeftSuperiorLowerExtremityExam.mov","leftsuperiorlowerextremityexamaudio");
}
else if(StepNumber==20)
{
SetVariables(true, "Record Left shin 2 feet Anterior to Shin after firmly pressing with finger","LeftAnteriorLowerExtremityExam.mov","leftanteriorlowerextremityexamaudio");
}
else
{
}
////////////////////////END CUSTOM IMPLEMENTATION///////////////////////
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.exam, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_exam, container,
false);
return rootView;
}
}
}
这是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"
android:orientation="vertical"
android:id="@+id/relativeLayoutid"
android:background="@drawable/manimagebasefront2"
tools:context="com.telehealthcaresolutions.virtualphysical.ExamActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textViewExam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="gravity"
android:gravity="center"
android:textColor="@color/white"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/Button01"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignLeft="@+id/textViewExam"
android:layout_alignParentBottom="true"
android:background="@drawable/button"
android:minHeight="0dp"
android:minWidth="0dp"
android:onClick="goBack"
android:text="Back"
android:textColor="@color/white" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignBaseline="@+id/Button01"
android:layout_alignBottom="@+id/Button01"
android:layout_alignRight="@+id/textViewExam"
android:background="@drawable/button"
android:minHeight="0dp"
android:minWidth="0dp"
android:onClick="goToNext"
android:text="Next"
android:textColor="@color/white" />
</RelativeLayout>
以下是Log Cat输出:
05-29 11:23:28.406: E/ActivityThread(18288): Performing stop of activity that is not resumed: {com.telehealthcaresolutions.virtualphysical/com.telehealthcaresolutions.virtualphysical.LoginPageActivity}
05-29 11:23:28.406: E/ActivityThread(18288): java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.telehealthcaresolutions.virtualphysical/com.telehealthcaresolutions.virtualphysical.LoginPageActivity}
05-29 11:23:28.406: E/ActivityThread(18288): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3333)
05-29 11:23:28.406: E/ActivityThread(18288): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3420)
05-29 11:23:28.406: E/ActivityThread(18288): at android.app.ActivityThread.access$1200(ActivityThread.java:161)
05-29 11:23:28.406: E/ActivityThread(18288): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
05-29 11:23:28.406: E/ActivityThread(18288): at android.os.Handler.dispatchMessage(Handler.java:102)
05-29 11:23:28.406: E/ActivityThread(18288): at android.os.Looper.loop(Looper.java:157)
05-29 11:23:28.406: E/ActivityThread(18288): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-29 11:23:28.406: E/ActivityThread(18288): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 11:23:28.406: E/ActivityThread(18288): at java.lang.reflect.Method.invoke(Method.java:515)
05-29 11:23:28.406: E/ActivityThread(18288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-29 11:23:28.406: E/ActivityThread(18288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-29 11:23:28.406: E/ActivityThread(18288): at dalvik.system.NativeStart.main(Native Method)
05-29 11:23:31.188: W/ApplicationPackageManager(18288): getCSCPackageItemText()
05-29 11:23:33.230: W/ApplicationPackageManager(18288): getCSCPackageItemText()
05-29 11:23:33.260: D/AndroidRuntime(18288): Shutting down VM
05-29 11:23:33.260: W/dalvikvm(18288): threadid=1: thread exiting with uncaught exception (group=0x4172ada0)
05-29 11:23:33.260: E/AndroidRuntime(18288): FATAL EXCEPTION: main
05-29 11:23:33.260: E/AndroidRuntime(18288): Process: com.telehealthcaresolutions.virtualphysical, PID: 18288
05-29 11:23:33.260: E/AndroidRuntime(18288): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.telehealthcaresolutions.virtualphysical/com.telehealthcaresolutions.virtualphysical.ExamActivity}: java.lang.NullPointerException
05-29 11:23:33.260: E/AndroidRuntime(18288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
05-29 11:23:33.260: E/AndroidRuntime(18288): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
05-29 11:23:33.260: E/AndroidRuntime(18288): at android.app.ActivityThread.access$900(ActivityThread.java:161)
05-29 11:23:33.260: E/AndroidRuntime(18288): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
05-29 11:23:33.260: E/AndroidRuntime(18288): at android.os.Handler.dispatchMessage(Handler.java:102)
05-29 11:23:33.260: E/AndroidRuntime(18288): at android.os.Looper.loop(Looper.java:157)
05-29 11:23:33.260: E/AndroidRuntime(18288): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-29 11:23:33.260: E/AndroidRuntime(18288): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 11:23:33.260: E/AndroidRuntime(18288): at java.lang.reflect.Method.invoke(Method.java:515)
05-29 11:23:33.260: E/AndroidRuntime(18288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-29 11:23:33.260: E/AndroidRuntime(18288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-29 11:23:33.260: E/AndroidRuntime(18288): at dalvik.system.NativeStart.main(Native Method)
05-29 11:23:33.260: E/AndroidRuntime(18288): Caused by: java.lang.NullPointerException
05-29 11:23:33.260: E/AndroidRuntime(18288): at com.telehealthcaresolutions.virtualphysical.ExamActivity.SetVariables(ExamActivity.java:108)
05-29 11:23:33.260: E/AndroidRuntime(18288): at com.telehealthcaresolutions.virtualphysical.ExamActivity.onCreate(ExamActivity.java:152)
05-29 11:23:33.260: E/AndroidRuntime(18288): at android.app.Activity.performCreate(Activity.java:5426)
05-29 11:23:33.260: E/AndroidRuntime(18288): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-29 11:23:33.260: E/AndroidRuntime(18288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
05-29 11:23:33.260: E/AndroidRuntime(18288): ... 11 more
编辑:我将我拥有的所有内容从onCreate()移动到OnViewCreate()。但它说我“无法对非静态方法SetVariables进行静态引用”
所以我创建了一个活动实例,并尝试从OnviewCreate调用SetVariables函数。我不知道如何将新创建的活动设置为我正在处理的当前活动。该应用程序仍然在“ExamActivity”活动中崩溃。以下是更改:
//i know this line is not correct... i don't know how to set it equal to my current activity?
ExamActivity myactivity= new ExamActivity();
SharedPreferences preferences = myactivity.getSharedPreferences("myPrefs", MODE_PRIVATE);
//get the selected gender
boolean gender =preferences.getBoolean("gender", false);
//get the step number.
int StepNumber= preferences.getInt("stepnumber", 1);
//if it is female
if(gender)
{
RelativeLayout rl = (RelativeLayout) myactivity.findViewById(R.id.relativeLayoutid);
rl.setBackgroundResource(R.drawable.womanbasefront2);
}
//check which step in the exam we are on.
if(StepNumber == 1)
{
myactivity.SetVariables(true,"General Head To Toe Pan", "GeneralExam.mov","generalexamaudio");
}
else if(StepNumber==2)
{
myactivity.SetVariables(true, "Examine Oral Mucosa" ,"OralMucosa.mov","oralmucosaexamaudio");
} .......
这消除了关于需要该功能是静态的错误消息,但应用程序仍然崩溃!我将SetVariables函数更改为静态函数,但这只会产生更多错误。新的Log Cat错误:
05-29 14:40:33.196: E/ActivityThread(8047): Performing stop of activity that is not resumed: {com.telehealthcaresolutions.virtualphysical/com.telehealthcaresolutions.virtualphysical.MainMenuActivity}
05-29 14:40:33.196: E/ActivityThread(8047): java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.telehealthcaresolutions.virtualphysical/com.telehealthcaresolutions.virtualphysical.MainMenuActivity}
05-29 14:40:33.196: E/ActivityThread(8047): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3333)
05-29 14:40:33.196: E/ActivityThread(8047): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3420)
05-29 14:40:33.196: E/ActivityThread(8047): at android.app.ActivityThread.access$1200(ActivityThread.java:161)
05-29 14:40:33.196: E/ActivityThread(8047): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
05-29 14:40:33.196: E/ActivityThread(8047): at android.os.Handler.dispatchMessage(Handler.java:102)
05-29 14:40:33.196: E/ActivityThread(8047): at android.os.Looper.loop(Looper.java:157)
05-29 14:40:33.196: E/ActivityThread(8047): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-29 14:40:33.196: E/ActivityThread(8047): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 14:40:33.196: E/ActivityThread(8047): at java.lang.reflect.Method.invoke(Method.java:515)
05-29 14:40:33.196: E/ActivityThread(8047): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-29 14:40:33.196: E/ActivityThread(8047): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-29 14:40:33.196: E/ActivityThread(8047): at dalvik.system.NativeStart.main(Native Method)
05-29 14:40:33.656: W/ApplicationPackageManager(8047): getCSCPackageItemText()
05-29 14:40:33.686: D/AndroidRuntime(8047): Shutting down VM
05-29 14:40:33.686: W/dalvikvm(8047): threadid=1: thread exiting with uncaught exception (group=0x4172ada0)
05-29 14:40:33.696: E/AndroidRuntime(8047): FATAL EXCEPTION: main
05-29 14:40:33.696: E/AndroidRuntime(8047): Process: com.telehealthcaresolutions.virtualphysical, PID: 8047
05-29 14:40:33.696: E/AndroidRuntime(8047): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.telehealthcaresolutions.virtualphysical/com.telehealthcaresolutions.virtualphysical.ExamActivity}: java.lang.NullPointerException
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.app.ActivityThread.access$900(ActivityThread.java:161)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.os.Handler.dispatchMessage(Handler.java:102)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.os.Looper.loop(Looper.java:157)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-29 14:40:33.696: E/AndroidRuntime(8047): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 14:40:33.696: E/AndroidRuntime(8047): at java.lang.reflect.Method.invoke(Method.java:515)
05-29 14:40:33.696: E/AndroidRuntime(8047): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-29 14:40:33.696: E/AndroidRuntime(8047): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-29 14:40:33.696: E/AndroidRuntime(8047): at dalvik.system.NativeStart.main(Native Method)
05-29 14:40:33.696: E/AndroidRuntime(8047): Caused by: java.lang.NullPointerException
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:185)
05-29 14:40:33.696: E/AndroidRuntime(8047): at com.telehealthcaresolutions.virtualphysical.ExamActivity$PlaceholderFragment.onCreateView(ExamActivity.java:180)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1189)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.app.Activity.performStart(Activity.java:5436)
05-29 14:40:33.696: E/AndroidRuntime(8047): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
05-29 14:40:33.696: E/AndroidRuntime(8047): ... 11 more
我很困惑为什么这不起作用。它说从getSharedPreferences获取值有一些问题。 。 。但我不知道如何解决它!
非常感谢任何帮助或建议。