0

我是一个Android新手,并使用asynctask和Java我试图创建一个即时消息系统。但这就是我所得到的。Android - 致命的例外:主 - 无法启动活动ComponentInfo nullpointerexeption

我被困在这个星期了。任何人都可以追踪我出错的地方,以及为什么会出现错误?

IndividualActivity.java

package com.example.soc; 
import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.os.Bundle; 
import android.view.Menu; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
public class IndividualActivity extends Activity { 

    private ProgressDialog pDialog; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getLayoutInflater().inflate(R.layout.individual, null); 
     Intent intent = getIntent(); 
     int uuid = intent.getIntExtra("uuid",0); //if it's a string you stored. 
     new openMsg(IndividualActivity.this).execute(); 

    } 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     hidePDialog(); 
    } 
    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    }  

} 

openMsg.java

package com.example.soc; 


import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.NameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.example.soc.adater.msgAdapter; 
import com.example.soc.model.MSList; 
import com.example.soc.util.JSONParser; 



public class openMsg extends AsyncTask<Object, Object, JSONArray>{ 
    final IndividualActivity main; 

    public openMsg(IndividualActivity indv) { 
     this.main = indv; 
    } 

    private JSONParser jsonParser = new JSONParser();  
    //private static final String TAG = MainActivity.class.getSimpleName(); 

    private ProgressDialog pDialog; 
    private List<MSList> imlist = new ArrayList<MSList>(); 
    private ListView listView; 
    private msgAdapter adapter; 
    String myip = "10.0.2.2"; 
    private JSONArray json = null; 
    ArrayList<Integer> uidList = new ArrayList<Integer>(); 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

      listView = (ListView)main.findViewById(R.id.listx); 
      adapter = new msgAdapter(main, imlist); 
      listView.setAdapter(adapter);  
    } 

    protected JSONArray doInBackground(Object... v) { 
     List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
     json = jsonParser.getJSONFromUrl("http://"+myip+"/resource/android/MSG.php", pairs); 
     return json; 
    } 

    protected void onPostExecute(JSONArray json) { 

     for (int i = 0; i < json.length(); i++) { 
      try { 
       JSONObject c = json.getJSONObject(i); 
       MSList iml = new MSList(); 
       iml.setpname(c.getString("name")); 
       //iml.setuname(c.getString("username")); 
       iml.setppUrl("http://"+myip+"/"+c.getString("img")); 
       iml.setmsgcon(c.getString("content")); 
       iml.setmsgtime(c.getString("time")); 
       int uuid = Integer.parseInt(c.getString("user_id")); 
       iml.setUid(uuid); 
       iml.setme(Integer.parseInt(c.getString("me"))); 

       imlist.add(iml); 

       uidList.add(uuid); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     adapter.notifyDataSetChanged();  
    } 

} 

Individual.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" 
    tools:context=".IndividualActivity" > 

    <ListView 
     android:id="@+id/listx" 
     android:layout_width="fill_parent" 
     android:layout_height="350dp" 
     android:layout_above="@+id/individual_bottomAction" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp" 
     android:listSelector="@drawable/list_row_selector" /> 

    <LinearLayout 
     android:id="@+id/individual_bottomAction" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" > 

     <EditText 
      android:id="@+id/msgTypeBox" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:layout_weight=".70" 
      android:ems="10" 
      android:hint="Write a message" 
      android:inputType="textMultiLine" /> 

     <Button 
      android:id="@+id/msgSend" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="14dp" 
      android:layout_weight=".2" 
      android:text="Send" /> 
    </LinearLayout> 

</RelativeLayout> 

logcat的

02-24 19:22:47.399: E/AndroidRuntime(6255): FATAL EXCEPTION: main 
02-24 19:22:47.399: E/AndroidRuntime(6255): Process: com.example.soc, PID: 6255 
02-24 19:22:47.399: E/AndroidRuntime(6255): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.soc/com.example.soc.IndividualActivity}: java.lang.NullPointerException 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.os.Handler.dispatchMessage(Handler.java:102) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.os.Looper.loop(Looper.java:136) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.app.ActivityThread.main(ActivityThread.java:5021) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at java.lang.reflect.Method.invoke(Method.java:515) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at dalvik.system.NativeStart.main(Native Method) 
02-24 19:22:47.399: E/AndroidRuntime(6255): Caused by: java.lang.NullPointerException 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at com.example.soc.openMsg.onPreExecute(openMsg.java:60) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.os.AsyncTask.execute(AsyncTask.java:535) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at com.example.soc.IndividualActivity.onCreate(IndividualActivity.java:37) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.app.Activity.performCreate(Activity.java:5231) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
02-24 19:22:47.399: E/AndroidRuntime(6255):  ... 11 more 
+1

什么就'OpenMsg'班线60号? – 2015-02-24 14:05:41

+0

其'listView.setAdapter(适配器);'\t \t @YuvaRaj – 2015-02-24 14:06:44

+0

请学习android的基础知识......你确实夸大了视图,但你没有将它设置为内容视图 – Selvin 2015-02-24 14:06:50

回答

0

尝试改变

getLayoutInflater().inflate(R.layout.individual, null); 

setContentView(R.layout.individual); 

让我知道,如果它

+0

它的工作原理,但为什么?完美理由的答案接受... – 2015-02-24 14:23:14

相关问题