如果有人能帮忙解决这个问题会很棒!
我一直得到一个空指针错误,这是奇怪的,因为应用程序在几分钟前工作正常,我看不出有什么问题,它在启动时崩溃了。如果您还需要其他任何有用的信息,请告知我们,以下是所有细节:
logcat的:
04-08 23:31:27.579 5906-5906/com.yupo.dominic.yupo E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yupo.dominic.yupo/com.yupo.dominic.yupo.LectureLogged}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.yupo.dominic.yupo.MenuFragment1.onCreateView(MenuFragment1.java:49)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5143)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
清单文件(如果它有用):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yupo.dominic.yupo" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".UserAuth"
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=".Login"
android:label="@string/title_login_area">
</activity>
<activity
android:name=".StudentLogged"
android:label="@string/title_student_area">
</activity>
<activity
android:name=".Register"
android:label="@string/register">
</activity>
<activity
android:name=".LectureLogged"
android:label="@string/title_lecture_area">
</activity>
<activity android:name=".MessagingActivity"
android:screenOrientation="portrait">
</activity>
<service android:name=".MessageService">
</service>
</application>
MenuFragment1类:
package com.yupo.dominic.yupo;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
import com.parse.ParseUser;
/**
* Created by Dominic on 03/03/2015.
*/
public class MenuFragment1 extends Fragment implements View.OnClickListener{
View rootview;
Spinner uniSpinner;
Button changeUniButton;
String uniSpinnerText;
ParseUser currentUser;
ParseQueryAdapter<ParseObject> adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
currentUser = ParseUser.getCurrentUser();
if(currentUser.getString("type").equalsIgnoreCase("Lecturer"))
{
rootview = inflater.inflate(R.layout.menu1_lecture, container, false);
TextView lectureUniTextView = (TextView) rootview.findViewById(R.id.lectureUniTextView);
TextView lectureChangeUniTextView = (TextView) rootview.findViewById(R.id.lectureChangeUniTextView);
uniSpinner = (Spinner) rootview.findViewById(R.id.uniSpinner);
uniSpinnerSetup();
changeUniButton = (Button) rootview.findViewById(R.id.changeUniButton);
changeUniButton.setOnClickListener(this);
if(currentUser.getString("university").equals(""))
{
lectureUniTextView.setText("You do not have a University set!");
lectureChangeUniTextView.setText("Select a University");
changeUniButton.setText("Set");
}
else
{
lectureUniTextView.setText("Your current University is: " + currentUser.getString("university"));
lectureChangeUniTextView.setText("Change University to");
changeUniButton.setText("Change");
}
}
else
{
rootview = inflater.inflate(R.layout.menu1_student, container, false);
TextView studentUniTextView = (TextView) rootview.findViewById(R.id.studentUniTextView);
TextView studentChangeUniTextView = (TextView) rootview.findViewById(R.id.studentChangeUniTextView);
uniSpinner = (Spinner) rootview.findViewById(R.id.uniSpinner);
uniSpinnerSetup();
changeUniButton = (Button) rootview.findViewById(R.id.changeUniButton);
changeUniButton.setOnClickListener(this);
if(currentUser.getString("university").equals(""))
{
studentUniTextView.setText("You do not have a University set!");
studentChangeUniTextView.setText("Select a University");
changeUniButton.setText("Set");
}
else
{
studentUniTextView.setText("Your current University is: " + currentUser.getString("university"));
studentChangeUniTextView.setText("Change University to");
changeUniButton.setText("Change");
}
}
return rootview;
}
public void uniSpinnerSetup()
{
ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("University");
return query;
}
};
adapter = new ParseQueryAdapter<ParseObject>(getActivity(), factory);
adapter.setTextKey("name");
uniSpinner.setAdapter(adapter);
uniSpinner.setSelection(1);
uniSpinner.setOnItemSelectedListener(new mySpinnerListener());
}
class mySpinnerListener implements Spinner.OnItemSelectedListener
{
@Override
public void onItemSelected(AdapterView parent, View v, int position,long id) {
ParseObject theSelectedObject = adapter.getItem(position);
uniSpinnerText = theSelectedObject.get("name").toString();
}
@Override
public void onNothingSelected(AdapterView parent) {
// TODO Auto-generated method stub
// Do nothing.
}
}
@Override
public void onClick(View v) {
if(currentUser.getString("university").equals(uniSpinnerText))
{
Toast.makeText(getActivity(),"Your University is already set to that!", Toast.LENGTH_LONG).show();
}
else
{
currentUser.put("university", uniSpinnerText);
currentUser.saveInBackground();
Toast.makeText(getActivity(), "University changed to: " + uniSpinnerText, Toast.LENGTH_LONG).show();
}
}
}
menu1_student.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/selectUniFrame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Your Selected University: "
android:id="@+id/studentUniTextView"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/changeUni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/selectUniFrame">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Change University to"
android:id="@+id/studentChangeUniTextView"
android:layout_alignParentLeft="true"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/uniSpinner"
android:visibility="visible"
android:layout_below="@+id/studentChangeUniTextView"
android:spinnerMode="dropdown"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/changeUniButton"
android:layout_toRightOf="@+id/uniSpinner"
android:text="Change"/>
</RelativeLayout>
</RelativeLayout>
menu1_lecture.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/selectUniFrame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Your Selected University: "
android:id="@+id/lectureUniTextView"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/changeUni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/selectUniFrame">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Change University to"
android:id="@+id/lectureChangeUniTextView"
android:layout_alignParentLeft="true"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/uniSpinner"
android:visibility="visible"
android:layout_below="@+id/lectureChangeUniTextView"
android:spinnerMode="dropdown"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/changeUniButton"
android:layout_toRightOf="@+id/uniSpinner"
android:text="Change"/>
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:0)
而不是
if(currentUser.getString("university").equals(""))
使用
if(currentUser==null || currentUser.getString("university")==null || currentUser.getString("university").equals(""))
答案 1 :(得分:0)
您的university
对象中似乎没有currentUser
个键。
它可能只在某一点上填充,这就是它之前工作的原因。
如果您卸载/重新安装,或者您在某处清除了某些代码,那么它就不会填充。
要回答有关预期(未定义)行为的问题,请参阅Parse文档:
getString
public String getString(String key)
访问字符串值。
参数:key - 访问值的键。
返回:如果没有这样的键或者它不是String
,则返回null
因此,在这种情况下返回null是预期的行为。
参考:https://parse.com/docs/android/api/com/parse/ParseObject.html#getString(java.lang.String)