Android:RuntimeException:无法启动活动ComponentInfo:NullpointerException

时间:2014-04-27 14:30:36

标签: android android-fragments

虽然这个主题有很多问题,但这些解决方案都没有对我的案例有所帮助。

我有一个列表片段,如下所示,必须将一些参数传递给详细视图(另一个活动)

看起来我的EditTextTextView我尝试在详细信息视图中设置值为空。

但是,我检查了布局是否存在,并且TextView定义了相同的ID。

请帮我找到错误。

  

ViewBookmarksFragment.java

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Log.i("bookmarks-onItemCLick", position + " - " + id);
    String item = (String) getListAdapter().getItem(position);
    Toast.makeText(getActivity(), item + " selected", Toast.LENGTH_LONG).show();
    Intent intent = new Intent(getActivity(), BookmarkActivity.class);
    intent.putExtra("bookmark", bookmarks.get(position));
    startActivity(intent);
}
  

BookmarkActivity.java

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

    Bundle extras = getIntent().getExtras();
    String bookmark = "Oops! No popcorn for you.";
    if (extras != null) {
        bookmark = extras.getString("bookmark");
    }
    JSONObject jobj;
    try {
        bName = (EditText) findViewById(R.id.bookmark_name);
        jobj = new JSONObject(bookmark);
        String txt = jobj.getString("name");
        Log.i("Test", txt);
        if(bName == null) {
            Log.i(":(", "Problem here");
        }
                      /*** Error from below line***/
        bName.setText(txt);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}
  

fragment_bookmark.xml

 <EditText
    android:id="@+id/bookmark_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="20dp"
    android:lines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"/> 

1 个答案:

答案 0 :(得分:2)

试试这个..

改变这个..

setContentView(R.layout.activity_bookmark);

setContentView(R.layout.fragment_bookmark);

并删除

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment())
            .commit();
}