编译代码时出错

时间:2014-08-04 21:14:23

标签: java android json android-layout android-activity

我收到了来自logcat的以下消息。我已经清理了我的项目,并且已经验证了清单中引用了活动。我相信这可能是活动代码的问题。我在这里包含了logcat messaage和活动代码。

我也在寻找潜在的解决方案,但到目前为止我的尝试都没有成功。

提前致谢

Logcat消息:

08-04 20:00:30.492: E/AndroidRuntime(1481): FATAL EXCEPTION: main
08-04 20:00:30.492: E/AndroidRuntime(1481): Process: com.dooba.beta, PID: 1481
08-04 20:00:30.492: E/AndroidRuntime(1481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.EventsActivity}: java.lang.NullPointerException
08-04 20:00:30.492: E/AndroidRuntime(1481):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at android.os.Looper.loop(Looper.java:136)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at java.lang.reflect.Method.invokeNative(Native Method)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at java.lang.reflect.Method.invoke(Method.java:515)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at dalvik.system.NativeStart.main(Native Method)
08-04 20:00:30.492: E/AndroidRuntime(1481): Caused by: java.lang.NullPointerException
08-04 20:00:30.492: E/AndroidRuntime(1481):     at com.dooba.beta.EventsActivity.onCreate(EventsActivity.java:54)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at android.app.Activity.performCreate(Activity.java:5231)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-04 20:00:30.492: E/AndroidRuntime(1481):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-04 20:00:30.492: E/AndroidRuntime(1481):     ... 11 more

活动代码:

 public class EventsActivity extends Activity{

        private static final String URL_WEB_SERVICE = "http://dooba.ca/analytics/ed.php";
        private GridView gv;
        private ArrayList<Events_List> container;
        private ArrayList<Events_List> items;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.events_list_layout);
            gv = (GridView) findViewById(R.id.gridview);
            container = new ArrayList<Events_List>();
            //download JSON
            listDownload();


            ListView s = (ListView) findViewById(R.id.listViewSingleClick);
            s.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(EventsActivity.this,EventSingleItemActivity.class);
                    intent.putExtra("id_item", id);//this is id of the item that you said it. for example id:5544323 from your JSON web service
                    intent.putExtra("position", position); //order position in listview 0-1-2-3...
                    startActivity(intent); //start Activity
                }
            });
        }
        public void listDownload(){
            RequestQueue volley = Volley.newRequestQueue(this);
            JsonObjectRequest json = new JsonObjectRequest(Method.GET, URL_WEB_SERVICE, null, ResponseListener(), ErrorListener());
            volley.add(json);
        }
private Response.Listener<JSONObject> ResponseListener() {
        return new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    //your JSON Array
                    JSONArray array = response.getJSONArray("list_item");
                    for(int i = 0; i < array.length(); i++){
                        container.add(convertirAnuncio(array.getJSONObject(i)));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                gv.setAdapter(new AdapterEvents(getApplicationContext(),container));
                }
            };
        };


    private Response.ErrorListener ErrorListener() {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) { }
        };
    }

    //object JSON
    private final Events_List convertirAnuncio(JSONObject obj) throws JSONException {
        long id = obj.getLong("id"); //id 
        String list_item_name = obj.getString("list_item_name"); 
        String list_item_description = obj.getString("list_item_description");
        String list_item_price = obj.getString("list_item_price");
        Uri uri = Uri.parse(obj.getString("list_item_bac"));
        return new Events_List(id,list_item_name,list_item_description,list_item_price, uri);
    }
}

2 个答案:

答案 0 :(得分:1)

转到EventsActivity.java中代码的第54行,查看您正在使用的参考资料。

我强烈怀疑第54行是第二行:

ListView s = (ListView) findViewById(R.id.listViewSingleClick);
s.setOnItemClickListener(new OnItemClickListener(){..}

这意味着findViewById找不到listViewSingleClick,这通常意味着您没有在events_list_layout布局XML文件中定义此ID。进入XML文件并确保您定义了一个标识为listViewSingleClick的元素。

答案 1 :(得分:0)

我刚才,我犯了这个愚蠢的错误,包括&#34;。&#34;在Manifest文件中的活动名称中。由于你没有发布清单文件,我只是在假设。你能检查一下是否是这种情况吗?

<activity
   android:name=".EventsActivity"
   android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.ALTERNATIVE" />
    </intent-filter>
</activity>