2015-09-13 54 views
0

我對我的啓動畫面有第一個活動,第二個活動是從我的導航可繪製的活動擴展而來的。所以問題是當我去第二個活動,去處理導航位置的函數,我得到空指針錯誤。我不知道爲什麼是這樣的行爲。在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); 
} 

此代碼調用第一位置時的動作活動第一次出現

+0

請張貼logcat的錯誤? –

+0

@AhmadAlkhateeb我只是添加logcat。 thx;) –

+0

其中** showInfo **是?它應該怎麼做? –

回答

0

我的客人是,你沒有在onCreate方法中初始化你的mDrawerLayout,可能是在onCreate方法中從下面行提升的異常......!

mDrawerLayout.addView(contentView, 0); 

請先initilialize的mDrawerLayout,像下面,

mDrawerLayout = (DrawerLayout) findViewById (R.id.your_drawer_layout_id); 

一個morething你是不是像下面活動設置內容,

setContentView(int); 
+0

thx迴應。但我發現我的問題在哪裏。但我不知道爲什麼這麼喜歡那個。我編輯我的問題 –

1

用來初始化mDrawerLayout並請嘗試:

showInfo = (TextView) contentView.findViewById(R.id.show_uniInfo); 

insted的的:

showInfo = (TextView) findViewById(R.id.show_uniInfo); 
+0

我測試過了,它沒有反應不愉快。但thx任何方式 –

+0

歡迎:) –