我有一个活动,并在此活动中使用Listview,ImageButton,Edittext和Spinner。但我的问题是,每当我运行我的代码时,它会显示错误..
08-11 06:38:38.433: D/dalvikvm(1012): GC_FOR_ALLOC freed 133K, 7% free 2745K/2940K, paused 172ms, total 206ms
08-11 06:38:38.713: E/TCP Client(1012): C: Connecting...
08-11 06:38:38.723: D/AndroidRuntime(1012): Shutting down VM
08-11 06:38:38.723: W/dalvikvm(1012): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-11 06:38:38.973: E/AndroidRuntime(1012): FATAL EXCEPTION: main
08-11 06:38:38.973: E/AndroidRuntime(1012): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android_examples.com.tabhost/com.android_examples.com.tabhost.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android_examples.com.tabhost/com.android_examples.com.tabhost.TabActivity_1}: java.lang.NullPointerException
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.os.Looper.loop(Looper.java:137)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-11 06:38:38.973: E/AndroidRuntime(1012): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 06:38:38.973: E/AndroidRuntime(1012): at java.lang.reflect.Method.invoke(Method.java:525)
08-11 06:38:38.973: E/AndroidRuntime(1012): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-11 06:38:38.973: E/AndroidRuntime(1012): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-11 06:38:38.973: E/AndroidRuntime(1012): at dalvik.system.NativeStart.main(Native Method)
08-11 06:38:38.973: E/AndroidRuntime(1012): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android_examples.com.tabhost/com.android_examples.com.tabhost.TabActivity_1}: java.lang.NullPointerException
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2054)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.widget.TabHost.addTab(TabHost.java:240)
08-11 06:38:38.973: E/AndroidRuntime(1012): at com.android_examples.com.tabhost.MainActivity.onCreate(MainActivity.java:44)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.Activity.performCreate(Activity.java:5133)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-11 06:38:38.973: E/AndroidRuntime(1012): ... 11 more
08-11 06:38:38.973: E/AndroidRuntime(1012): Caused by: java.lang.NullPointerException
08-11 06:38:38.973: E/AndroidRuntime(1012): at com.android_examples.com.tabhost.TabActivity_1.onCreate(TabActivity_1.java:50)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.Activity.performCreate(Activity.java:5133)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-11 06:38:38.973: E/AndroidRuntime(1012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-11 06:38:38.973: E/AndroidRuntime(1012): ... 21 more
08-11 06:38:46.273: I/Process(1012): Sending signal. PID: 1012 SIG: 9
我的活动代码是..
public class TabActivity_1 extends Activity {
private ListView mList;
private ArrayList<String> arrayList;
private MyCustomAdapter mAdapter;
private TCPClient mTcpClient;
Spinner sp1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_activity_1);
sp1=(Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> ar=ArrayAdapter.createFromResource(this,R.array.Type1,android.R.layout.simple_list_item_1);
ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp1.setAdapter(ar);
arrayList = new ArrayList<String>();
final EditText editText = (EditText) findViewById(R.id.editText1);
ImageButton startt = (ImageButton)findViewById(R.id.imageButton1);
//relate the listView from java to the one created in xml
mList = (ListView)findViewById(R.id.listView1);
mAdapter = new MyCustomAdapter(this, arrayList);
mList.setAdapter(mAdapter);
// connect to the server
new connectTask().execute("");
startt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = editText.getText().toString();
//add the text in the arrayList
arrayList.add("c: " + message);
//refresh the list
mAdapter.notifyDataSetChanged();
editText.setText("");
}
});
}
public class connectTask extends AsyncTask<String,String,TCPClient> {
@Override
protected TCPClient doInBackground(String... message) {
//we create a TCPClient object and
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
@Override
//here the messageReceived method is implemented
public void messageReceived(String message) {
//this method calls the onProgressUpdate
publishProgress(message);
}
});
mTcpClient.run();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
//in the arrayList we add the messaged received from server
arrayList.add(values[0]);
// notify the adapter that the data set has changed. This means that new message received
// from server was added to the list
mAdapter.notifyDataSetChanged();
}
}
public void buttonClickMethod(View v){
Intent i= new Intent(TabActivity_1.this,PageAjout.class);
startActivity(i);
}
}
我很困惑,我做错了。指导我。
答案 0 :(得分:0)
将空arrayList设置为适配器,这就是代码给出NullPointerException的原因。
从onCreate()
mAdapter = new MyCustomAdapter(this, arrayList);
mList.setAdapter(mAdapter);
并覆盖onPostExecute()
的{{1}}方法并将arrayList设置为adapter。您还可以设置条件以检查arrayList是否应为null。
AsyncTask
希望它有所帮助。