我使用的是Android Studio,在更新'Android Support Libary'后,我有问题要连接到Parse.com。 22。 之前工作的项目现在不起作用。 经过几个小时的搜索和测试,我决定只为此打开一个清晰的项目,但问题仍然存在。
MainActivity Class
public class MainActivity extends Activity {
List<ParseObject> data;
List<String> nameList;
String[] name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String appId = " ********* ";
String clientId = " ********* ";
Parse.initialize(getApplicationContext(), appId, clientId);
ParseQuery<ParseObject> query = ParseQuery.getQuery("test");
try {
data = query.find();
}catch (ParseException e){
Log.i("myParseExceptionLog", "Can't connect to test table at parse");
}
nameList = new ArrayList<String>();
for (int i=0 ; i < data.size(); i++){
nameList.add(data.get(i).getString("name"));
}
name = nameList.toArray(new String[nameList.size()]);
TextView myTV = (TextView)findViewById(R.id.test);
myTV.setText(name[0]);
}
}
activity_main.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=".MainActivity">
<TextView
android:id="@+id/test"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
AndroidManifast.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shachar.com.parsetest" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
的build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
我知道我需要在build.gradle中依赖我的jar,但请查看注释。 无论如何,它出现在Project Structure中,我可以在我的类中从parse导入libs。 当我尝试在build.gradle中依赖并同步Manifat时。
在黑屏2分钟后,应用程序记住崩溃并带给我NullPointerExpiton(因为'数据'为空)。
LogCat错误
03-17 16:46:17.686 18427-18427/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: shachar.com.parsetest, PID: 18427
java.lang.RuntimeException: Unable to start activity ComponentInfo{shachar.com.parsetest/shachar.com.parsetest.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
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:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at shachar.com.parsetest.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
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:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
03-17 16:46:18.212 672-695/? E/﹕ Could not open '/data/data/hotplug/cmd'
03-17 16:46:18.212 672-695/? E/﹕ error : 2, No such file or directory
03-17 16:46:18.245 672-3024/? E/Sensors﹕ new acc setDelay handle(0),ns(20000000) err! go to hwmsen
03-17 16:46:18.323 672-3023/? E/﹕ Could not open '/data/data/hotplug/cmd'
03-17 16:46:18.323 672-3023/? E/﹕ error : 2, No such file or directory
我想知道什么?
我错过了什么?
新的“Android支持Libary”版本会出现什么变化?
修改
我刚刚做了另一个测试,我从他们的网站上下载了一个complet exmple od解析器,其中包含所有设置和最后一个解析版本。我重新安装Android支持Libary但仍无法正常工作
答案 0 :(得分:0)
是的,看起来你在第39行有空值,你可以在这里添加行号,以便我们可以看到这个空值被抛出的位置吗?
答案 1 :(得分:0)
好的,如果for (int i=0 ; i < data.size(); i++)
产生NullPointerException,那肯定是因为data
为空,所以你必须检查data
是否为空。