我對我的啓動畫面有第一個活動,第二個活動是從我的導航可繪製的活動擴展而來的。所以問題是當我去第二個活動,去處理導航位置的函數,我得到空指針錯誤。我不知道爲什麼是這樣的行爲。在Android意圖之後的空指針
這是我的第一個活動代碼:
package com.example.uniapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SplashScreen extends Activity {
Button next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next = (Button) findViewById(R.id.button);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SplashScreen.this,Description.class);
startActivity(intent);
finish();
}
});
}
///////////////////////////////////////////////////////////////some function this functions is not use
public int testdb()
{
int count = 0;
SQLiteDatabase mydb;
try {
Toast.makeText(SplashScreen.this, "asdasddasds", Toast.LENGTH_LONG).show();
MyDatabase MyDataBase = new MyDatabase(this);
mydb = MyDataBase.getReadableDatabase();
Cursor cursor = mydb.rawQuery("SELECT * FROM news",null);
cursor.moveToFirst();
cursor.moveToNext();
String TempTest = cursor.getString(cursor.getColumnIndex("title"));
if(!TempTest.equals(""))
count = 5;
if(cursor != null)
{
//cursor.moveToFirst();
count = cursor.getInt(0);
Toast.makeText(getApplicationContext(), "null!!!!!!!!!!" , Toast.LENGTH_LONG).show();
}
cursor.close();
mydb.close();
Toast.makeText(getApplicationContext(), "database 11 done : " + count , Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO: exception daa base
Toast.makeText(getApplicationContext(), "data error 1 : " + 0, Toast.LENGTH_LONG).show();
}
return count;
}
}
,這是我的第二個活動代碼:
package com.example.uniapp;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Description extends DrawableNav implements OnItemClickListener {
int drawableposition;
MyArray myarray;
MyDatabase MyDataBase;
SQLiteDatabase mydb;
int count = 0;
ListView MyList;
TextView showInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//////////////////////////////////////////////////////////////////////////////////////////////////*inflaste layout
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflate your activity layout here!
View contentView = inflater.inflate(R.layout.newdescription, null, false);
mDrawerLayout.addView(contentView, 0);
//////////////////////////////intilitize list view
MyList = (ListView)findViewById(R.id.list);
showInfo = (TextView) findViewById(R.id.show_uniInfo);
//////////////////////////////get intent
//Intent intent = getIntent();
//count = intent.getIntExtra("db", 0);
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<cases
public void SelectItem(int possition) {
drawableposition = possition;
if(possition != 0)
showInfo.setVisibility(View.GONE);
switch (possition) {
case 0 :
System.out.println("<<<<<<<<<<<<<<<<<<<test for exception");
showInfo.setVisibility(View.VISIBLE); ///get null pointer here by all codes
break;
case 1:
////////////// codes work well
break;
}
}
}
我更加迷惑,有些吐司工作there.when我敬酒那裏沒有問題,但我不能使textview可見。
09-13 22:16:48.708:E/AndroidRuntime(28067):致命異常:主 09-13 22:16:48.708:E/AndroidRuntime(28067):進程:com.example.uniapp,PID :28067 09-13 22:16:48.708:E/AndroidRuntime(28067):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.uniapp/com.example.uniapp.Description}:java.lang。 NullPointerException 09-13 22:16:48.708:E/AndroidRuntime(28067):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404) 09-13 22:16:48.708:E/AndroidRuntime(28067)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) 09-13 22:16:48.708:E/AndroidRuntime(28067):at android.app.ActivityThread.access $ 900(ActivityThread.java:172) 09-13 22:16:48.708:E/AndroidRuntime(28067):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1308) 09-13 22:16:48.708:E/AndroidRuntime(28067) :at android.os.Handler.dispatchMessage(Handler.java:102) 09-13 22:16:48.708:E/AndroidRuntime(28067):at android.os.Looper.loop(Looper.java:146) 09 -13 22:16:48.708:E/AndroidRuntime(28067):在android.app.ActivityThread.main(ActivityThread.java:5653) 09-13 22:16:48.708:E/AndroidRuntime(28067):在java。 lang.reflect.Method.invokeNative(Native Method) 09-13 22:16:48.708:E/AndroidRuntime(28067):at java.lang.reflect.Method.invoke(Method.java:515) 09-13 22 :16:48.708:E/AndroidRuntime(28067):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1291) 09-13 22:16:48.708: E/AndroidRuntime(28067):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 09-13 22:16:48.708:E/AndroidRuntime(28067):at dalvik.system.NativeStart。 main(Native Method) 09-13 22:16:48.708:E/AndroidRuntime(28067):引起於:java.lang.NullPointerException 09-13 22:16:48.708:E/AndroidRuntime(28067):at com。 example.uniapp.Description.SelectItem(Description.java:94) 09-13 22:16:48.708:E/AndroidRuntime(28067):at com.example.uniapp.DrawableNav.onCreate(DrawableNav.java:109) 09 -13 22:16:48.708:E/AndroidRuntime(28067):at com.example.uniapp.Description.onCreate(Description.java:61) 09-13 22:16:48.708:E/AndroidRuntime(28067):at android.app.Activity.performCreate(Activity.java:5541) 09-13 22:16:48.708:E/AndroidRuntime(28067):at android.app.Instrumentat在Android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368) 09-13 22:16:E/AndroidRuntime(28067):ion.callActivityOnCreate(Instrumentation.java:1093) 09-13 22:16:48.708: 48.708:E/AndroidRuntime(28067):...11多
而且我發現,如果我從我的導航繪製活動抹掉這段代碼萬事都好
這是代碼:
if (savedInstanceState == null) {
SelectItem(0);
}
此代碼調用第一位置時的動作活動第一次出現
請張貼logcat的錯誤? –
@AhmadAlkhateeb我只是添加logcat。 thx;) –
其中** showInfo **是?它應該怎麼做? –