0

안드로이드의 멍청한 놈의 비트. 탐색 창을 사용하여 ListView를 가져 오는 중 ... setContentView 행의 주석 처리를 제거하지 않으면 다음이 작동하지 않습니다. null ptr 예외를 슬로우합니다. 주석 처리를 제거하면 ListView가 작동하지만 nav 드로어가 표시되지 않습니다. 또한 onListItemClick은 트리거하지 않지만 나중에 배우는 것이 문제입니다.내비게이션 서랍이 포함 된 조각 모음

나는이 순간에 볼 수있는 것보다 더 많은 것이 있지만, 어떤 도움을 환영합니다.

MainActivity

package com.hourtracker; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 
import android.support.v4.widget.DrawerLayout; 

public class MainActivity extends ActionBarActivity implements  NavigationDrawerFragment.NavigationDrawerCallbacks { 

/** 
* Fragment managing the behaviors, interactions and presentation of the 
* navigation drawer. 
*/ 
private NavigationDrawerFragment mNavigationDrawerFragment; 

/** 
* Used to store the last screen title. For use in 
* {@link #restoreActionBar()}. 
*/ 
private CharSequence mTitle; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.e("hours","creating session filter"); 
    setContentView(R.layout.activity_main); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.navigation_drawer); 
    mTitle = getTitle(); 

    // Set up the drawer. 
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer, 
      (DrawerLayout) findViewById(R.id.drawer_layout)); 
} 

@Override 
public void onNavigationDrawerItemSelected(int position) { 
    // update the main content by replacing fragments 
    Log.d("onNavDrawerSelected", String.valueOf(position)); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    switch(position) { 
    case 0: 
    fragmentManager 
      .beginTransaction() 
      .replace(R.id.frame_container, 
        ((Fragment)new Entry_Fragment())).commit(); 
    break; 
    case 1: 
    fragmentManager 
      .beginTransaction() 
      .replace(R.id.frame_container, 
        ((Fragment)new Week_Fragment())).commit(); 
    break; 
    case 2: 
    fragmentManager 
      .beginTransaction() 
      .replace(R.id.frame_container, 
        ((Fragment)new All_Listview())).commit(); 
    break; 
    } 
} 

public void onSectionAttached(int number) { 
    switch (number) { 
    case 1: 
     mTitle = getString(R.string.title_section1); 
     break; 
    case 2: 
     mTitle = getString(R.string.title_section2); 
     break; 
    case 3: 
     mTitle = getString(R.string.title_section3); 
     break; 
    } 
} 

public void restoreActionBar() { 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
    actionBar.setDisplayShowTitleEnabled(true); 
    actionBar.setTitle(mTitle); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    if (!mNavigationDrawerFragment.isDrawerOpen()) { 
     // Only show items in the action bar relevant to this screen 
     // if the drawer is not showing. Otherwise, let the drawer 
     // decide what to show in the action bar. 
     getMenuInflater().inflate(R.menu.main, menu); 
     restoreActionBar(); 
     return true; 
    } 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager 
     .beginTransaction() 
     .replace(R.id.frame_container, 
       ((Fragment)new Settings_Fragment())).commit(); 

     return true; 
    } 
    Toast.makeText(this, "Not settings", Toast.LENGTH_SHORT).show();; 
    return super.onOptionsItemSelected(item); 
} 

}

activity_main.xml

<!-- 
    As the main content view, the view below consumes the entire 
    space available using match_parent in both dimensions. 
--> 

<FrameLayout 
    android:id="@+id/frame_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<!-- 
    android:layout_gravity="start" tells DrawerLayout to treat 
    this as a sliding drawer on the left side for left-to-right 
    languages and on the right side for right-to-left languages. 
    If you're not building against API 17 or higher, use 
    android:layout_gravity="left" instead. 
--> 
<!-- 
    The drawer is given a fixed width in dp and extends the full height of 
    the container. 
--> 

<fragment 
    android:id="@+id/navigation_drawer" 
    android:name="com.hourtracker.NavigationDrawerFragment" 
    android:layout_width="@dimen/navigation_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" /> 

</android.support.v4.widget.DrawerLayout> 

all_listview.xml

<?xml version="1.0" encoding="utf-8"?> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/all_list_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</ListView> 
+0

활동 코드를 게시 할 수 있습니까? 그 널 포인터 예외는 어디에 있습니까? –

+0

setContentView가 주석 처리 된 lv.setAdapter (어댑터) 행에 NullPointer가 표시됩니다 (Eclipse에서 디버깅하는 것은 lv가 null임을 보여줍니다). 주석을 제거하면 완벽하게 작동하지만 탐색 서랍은 아무 것도하지 않습니다. MainActivity는 무언가가 클릭되었음을 나타내지 만 서랍을 열지 않습니다. – emaNoN

+0

setContentView를 가지고 있기 때문에 id : all_list_view를 찾을 수 없기 때문에 null이됩니다. –

답변

0

나는 주된 오류가 ListFragment를 확장하는 All_Listview를 만들 때라고 생각한다. 간단한 ListFragment를 생성하는 데 도움이되는 follow this example을 제안한다.

바로 뒤에 나는 조각 조각이 작동하고 탐색 서랍도 작동한다고 생각한다. ListFragment에서 ContentView를 설정할 때 잘못했기 때문에

+0

그래서 시도하기 전에 MainActivity에서 처리해야한다고 말하고 있습니까? – emaNoN

+0

All_Listview가 예제와 같아야합니다. Activity 클래스에 All_Listview를 작성하지 않아도됩니다. –

+0

onActivityCreated() 대신 onCreateVie()를 호출해야했습니다. – emaNoN