为什么活动没有被实例化?

时间:2015-08-13 16:41:22

标签: android android-fragments android-intent android-activity

我在viewpager frament中有一个专辑的网格视图。我们的想法是在单击网格项目时在新窗口中显示相册的内容。这是我的代码:

在相册片段的onCreateView()方法内部:

    GridView grid = (GridView) getView().findViewById(R.id.grid);


    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView al = (TextView) view.findViewById(R.id.album_name);
            TextView ar = (TextView) view.findViewById(R.id.songs_num);

            String album_name = (String) al.getText();
            String artist_name = (String) ar.getText();
            startActivity(new Intent(getActivity().getApplicationContext(), DisplayAlbumContent.class).putExtra("pos", position).putExtra("album_name", album_name).putExtra("artist_name", artist_name));
        }
    });

这是DisplayAlbumContent.java:

public class DisplayAlbumContent extends Activity implements LoaderManager.LoaderCallbacks<Cursor>{
DisplaySongsAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.album_detail);



}
Intent i = getIntent();
Bundle b = i.getExtras();
String val[] = {(String) b.getCharSequence("album_name")};


static final String[] ALBUM_DETAIL_PROJECTION = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION};
String where = MediaStore.Audio.Media.ALBUM + "=?";




public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String orderby = MediaStore.Audio.Media.TITLE;
    return new CursorLoader(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,ALBUM_DETAIL_PROJECTION, where, val, orderby);
}



public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
}



public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.swapCursor(null);
}
}

但是我收到以下错误:

    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.tekinarslan.material.sample/com.screens.DisplayAlbumContent}: java.lang.NullPointerException
        at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.screens.DisplayAlbumContent.<init>(DisplayAlbumContent.java:29)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1215)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

假设您发布了准确版本的代码,这将导致问题:

Intent i = getIntent();
Bundle b = i.getExtras();
String val[] = {(String) b.getCharSequence("album_name")};

这些在类中声明为字段,这意味着它们将在创建类实例时初始化。但是当Android为您创建此类的实例时,它尚未设置意图。 这意味着i可能会从null获得值getIntent(),导致NPE在调用i.getExtras()时出现在下一行。

您需要将这些字段的初始化移到onCreate()方法中。此时,Intent将在您的Activity实例中设置。

public class DisplayAlbumContent extends Activity implements LoaderManager.LoaderCallbacks<Cursor>{
DisplaySongsAdapter mAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.album_detail);

      this.i = getIntent();
      this.b = i.getExtras();
      this.val = new String[] {(String) this.b.getCharSequence("album_name")};
  }

  Intent i;
  Bundle b;
  String[] val;

}

其他几点说明:

  • 你应养成使用稍微更具描述性的习惯 变量的名称;很多ibval得到了 很快就混淆了。
  • 在Java中也是常规的 类型旁边的数组类型括号[],而不是旁边的 变量 - 正如我上面所做的那样(尽管它们具有相同的效果)。
  • 记下来自@DroidMind的评论 - logcat有助于告诉你在哪里 错误是,它应该为您提供如何修复它的线索。