2014-09-23 45 views
-2

我在執行我的應用時遇到了該錯誤。目標是將活動添加到列表中,並通過按列表中的按鈕來訪問該活動。書籤活動中的NullpointExcepction錯誤

我從教程中獲取代碼,活動代碼中沒有錯誤。我可以將我的活動添加到「收藏夾」中,該活動列在其他活動中。但是當我嘗試通過按鈕輸入該活動時,它會拋出NullPointException錯誤。

這裏是活動的代碼:

public class BookmarksActivity extends Activity { 

private TextView mEmptyText; 
private LinearLayout mBookmarkLayout; 

private Context mContext; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.bookmarks); 

    mContext = getApplicationContext(); 

    mEmptyText = (TextView) findViewById(R.id.empty_textview); 
    mBookmarkLayout = (LinearLayout) findViewById(R.id.bookmark_insert_point); 

    getAllKeys(); 
} 

private void getAllKeys() 
{ 
    SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE); 
    Map<String,?> keys = sp.getAll(); 

    int count = 0; 
    for(Map.Entry<String,?> entry : keys.entrySet()) 
    { 
     String value = entry.getValue().toString(); 
     System.out.println("!!!!!!!!!!!!!!!!!value = "+value); 
     String delimiter = ","; 
     String[] values_array = value.split(delimiter); 
     addBookmark(values_array); 
     count++; //keep track of the number of bookmarks 
    } 

    //if there are no bookmarks, display a text view saying so. Otherwise, make the text view go away 
    if (count == 0) 
    { 
     mEmptyText.setVisibility(View.VISIBLE); 
     mEmptyText.setText(getString(R.string.no_bookmarks)); 
    } 
    else 
     mEmptyText.setVisibility(View.GONE); 

} 

private void addBookmark(String[] values_array) 
{  
    LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = vi.inflate(R.layout.single_bookmark, null); 

    TextView text = (TextView) v.findViewById(R.id.bookmark_text); 
    Button button = (Button) v.findViewById(R.id.bookmark_button); 

    text.setText(values_array[1]); 

    final String myClass = values_array[0]; 
    button.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Class<?> cl = null; 
      try { 
       cl = Class.forName(myClass); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 
      Intent myIntent = new Intent(mContext, cl); 
      startActivity(myIntent); 
     } 
    }); 

    // insert into main view 
    mBookmarkLayout.addView(v, 0, new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); 
    System.out.println("!!!!!!!!!!!!!!!!!!!!Just added a view"); 
} 
} 

我得到在誤差:意向myIntent =新意圖(mContext,CL);

也許myClass爲null,但我不知道是不是很清楚。

謝謝!

編輯:logcat的加入

09-23 20:04:53.367: E/AndroidRuntime(629): FATAL EXCEPTION: main 
09-23 20:04:53.367: E/AndroidRuntime(629): java.lang.NullPointerException 
09-23 20:04:53.367: E/AndroidRuntime(629): at android.content.ComponentName.<init>(ComponentName.java:76) 
09-23 20:04:53.367: E/AndroidRuntime(629): at android.content.Intent.<init>(Intent.java:3301) 
09-23 20:04:53.367: E/AndroidRuntime(629): at com.and.mememe.FavouritesActivity$1.onClick(FavouritesActivity.java:101) 
09-23 20:04:53.367: E/AndroidRuntime(629): at android.view.View.performClick(View.java:4084) 
09-23 20:04:53.367: E/AndroidRuntime(629): at android.view.View$PerformClick.run(View.java:16966) 
09-23 20:04:53.367: E/AndroidRuntime(629): at android.os.Handler.handleCallback(Handler.java:615) 
09-23 20:04:53.367: E/AndroidRuntime(629): at android.os.Handler.dispatchMessage(Handler.java:92) 
09-23 20:04:53.367: E/AndroidRuntime(629): at android.os.Looper.loop(Looper.java:137) 
09-23 20:04:53.367: E/AndroidRuntime(629): at android.app.ActivityThread.main(ActivityThread.java:4745) 
09-23 20:04:53.367: E/AndroidRuntime(629): at java.lang.reflect.Method.invokeNative(Native Method) 
09-23 20:04:53.367: E/AndroidRuntime(629): at java.lang.reflect.Method.invoke(Method.java:511) 
09-23 20:04:53.367: E/AndroidRuntime(629): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
09-23 20:04:53.367: E/AndroidRuntime(629): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
09-23 20:04:53.367: E/AndroidRuntime(629): at dalvik.system.NativeStart.main(Native Method) 

EDIT 2

button.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Class<?> cl = null; 
      try { 
       cl = Class.forName(myClass); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 
      Intent myIntent = new Intent(mContext, cl); 
      startActivity(myIntent); 
     } 
    }); 
+0

首先,你沒有在那裏得到'NPE'......發佈完整的logcat。其次,你不需要'應用程序上下文','活動上下文'將起作用。所以這個'mContext = getApplicationContext();'可以是'mContext = this;'但是這也不需要。只需使用'v.getContext()' – codeMagic 2014-09-23 19:25:06

+0

@codeMagic謝謝你的回答。我已經替換它並使用v.getContext(),但錯誤仍然存​​在。 – 2014-09-23 20:13:31

+0

你的問題出現在'onClick()'''FavouritesActivity.java'的第101行。這是你需要發佈的代碼。 – codeMagic 2014-09-23 20:34:05

回答

1

這裏的問題,最終是myClass沒有返回一個有效的類名。所以沒有Activity實際開始。

這裏該代碼

try { 
      cl = Class.forName(myClass); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 

被捉住ClassNotFoundException但代碼繼續嘗試啓動Activity這是無效的。要解決此問題,請將Intent代碼放入try塊中。這樣,當它捕捉到異常時,可以用它做一些事情,如記錄日誌,打印出消息等等。但是,最重要的是,它不會嘗試啓動無效的Activity。需要檢查myClass變量的有效性。它有錯誤的路徑或拼寫錯誤。

try { 
     cl = Class.forName(myClass); 
     Intent myIntent = new Intent(mContext, cl); 
     startActivity(myIntent); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    }