2011-10-08 5 views
0
public class Display extends ListActivity 
{ 
    private ForumAdapter faHelper; 
    private ProgressDialog dialog; 
    private String Url; 
    private int success = 0; 

    private SeparatedListAdapter sAdapter; 
    private ListView list; 

    private List<Map<String,?>> cat_list; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setTitle("Board Index"); 

     list = (ListView) findViewById(R.id.dlist); 
     list.setOnItemClickListener(itemclicklistener()); 

     setContentView(R.layout.displaylist); 

     faHelper = new ForumAdapter(this); 

     Url = geturl(savedInstanceState); 

     if (faHelper.isGood(Url)) 
     { 
      Load(); 
     } 
     else 
     { 
      alertdialog(0); 
     } 
    } 

    private class pbar extends AsyncTask<String, Void, Integer> 
    { 
     private final String message = "Loading. Please wait..."; 

     @Override 
     protected void onPreExecute() 
     { 
      dialog = ProgressDialog.show(Display.this, "", message, true); 
     } 

     @Override 
     protected void onPostExecute(Integer result) 
     { 
      success = result; 
      dialog.dismiss(); 
      displaystuff(); 
     } 

     protected Integer doInBackground(String... url) 
     { 
      return faHelper.SourceDownload(url[0]); 
     } 
    } 

    private void displaystuff() 
    { 
     if (success == 1) 
     { 
      sAdapter = new SeparatedListAdapter(this, R.layout.demoheader); 

      for (Object row : faHelper.BoardIndex) 
      { 
       String[][] theRow = (String[][])row; 
       String ctitle = ""; 
       cat_list = new LinkedList<Map<String,?>>(); 

       for (int i = 0; i < theRow.length; i++) 
       { 
        if (i == 0) ctitle = theRow[i][0]; 

        /* + "\n" + theRow[i][2]*/ 
        cat_list.add(createObject(theRow[i][1], theRow[i][3])); 
       } 

       ExAdapter exone = new ExAdapter(this, R.layout.demorow, cat_list); 
       sAdapter.addSection(ctitle, exone); 
      } 
      list.setAdapter(sAdapter); 
     } 
     else 
     { 
      alertdialog(1); 
     } 
    } 

    private OnItemClickListener itemclicklistener() 
    { 
     return new OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
      { 
       @SuppressWarnings("unchecked") // Object to Map. 
       Map<String,?> one = (Map<String,?>) parent.getAdapter().getItem(position); 
       Toast.makeText(getApplicationContext(), one.get("title").toString(), Toast.LENGTH_SHORT).show(); 
      } 
     }; 
    } 

    private void alertdialog(int which) 
    { 
     switch (which) 
     { 
     case 0: 
      new AlertDialog.Builder(this) 
      .setTitle("Alert") 
      .setMessage("Connection Error!!!" + "\n\n" + faHelper.rstring) 
      .setPositiveButton("OK", new OnClickListener() 
      { 
       public void onClick(DialogInterface arg0, int arg1) 
       { 
        Display.this.finish(); 
       } 
      }) 
      .show(); 
      break; 
     case 1: 
      new AlertDialog.Builder(this) 
      .setTitle("Alert") 
      .setMessage("Error!!" + "\n\n" + "Press OK to try again!") 
      .setPositiveButton("OK", new OnClickListener() 
      { 
       public void onClick(DialogInterface arg0, int arg1) 
       { 
        Load(); 
       } 
      }) 
      .setPositiveButton("Cancel", new OnClickListener() 
      { 
       public void onClick(DialogInterface arg0, int arg1) 
       { 
        success = 1; 
        Display.this.finish(); 
       } 
      }) 
      .show(); 
      break; 
     } 
    } 

    public Map<String,?> createObject(String title, String image) 
    { 
     Map<String,String> item = new HashMap<String,String>(); 
     item.put("title", title); 
     item.put("image", image); 
     return item; 
    } 

    public void refresh(View view) 
    { 
     Load(); 
    } 

    private void Load() 
    { 
     new pbar().execute(Url); 
    } 

    private String geturl(Bundle savedInstanceState) 
    { 
     String url = (savedInstanceState == null) ? null : (String) savedInstanceState.getSerializable("URL"); 

     if (url == null) 
     { 
      Bundle extras = getIntent().getExtras(); 
      url = extras != null ? extras.getString("URL") : null; 
     } 

     return url; 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) 
    { 
     super.onSaveInstanceState(outState); 
     outState.putSerializable("URL", Url); 
    } 

    @Override 
    protected void onPause() 
    { 
     super.onPause(); 
    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
    } 
} 

이 줄의 코드는 널 포인터 예외를 만드는 이유입니다. 그 이유는 모르겠습니다.null 포인터 예외에 대해 어떻게해야합니까?

list.setOnItemClickListener (itemclicklistener());

그냥 코드로 listview를 만들면 잘 작동하는 것 같습니다. 하지만 실제로 리니어 레이 아웃 내에서 목록 뷰로 설정하면 클릭 리스너를 제외한 모든 것이 정상입니다. 어떤 충고라도 도움이 될 것입니다. 다음과 같이

는 XML의는 다음과 같습니다

displaylist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical">  

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <Button 
      android:onClick="refresh" 
      android:text="Refresh" 
      android:id="@+id/refresh" 
      android:layout_width="match_parent" 
      android:layout_height="32dp"> 
     </Button> 
    </LinearLayout> 
    <ListView 
     android:id="@+id/dlist" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     </ListView> 
</LinearLayout> 

header.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/demoheader_title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="2dip" 
    android:paddingBottom="2dip" 
    android:paddingLeft="5dip" 
    android:background="#FFFFFF" 
    style="?android:attr/listSeparatorTextViewStyle" 
    /> 

row.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="45dp" 
     android:layout_height="45dp" 
     android:width="50dp" 
     android:height="50dp" 
     android:scaleType="fitStart" 
     android:paddingRight="5dp" 
     /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:singleLine="false" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     /> 
</LinearLayout> 
+1

글쎄, "고쳐". 질문이 너무 현지화되어 투표를 종료합니다. –

답변

1

된 setContentView (R.layout. 뭔가)이 처음 넣어 findviewbyid 함수를 작성한 후에이 널 포인터 예외를 찾을 수 없습니다.

0

findViewById() 전에 setContentView()을 호출해야합니다. 그렇지 않으면 널을 리턴합니다. 그것은 다음과 같습니다 귀하의 코드를 수정 :

setContentView(R.layout.displaylist); 

list = (ListView) findViewById(R.id.dlist); 
list.setOnItemClickListener(itemclicklistener()); 
1

당신은 당신은 당신이 활동으로 확장해야 ListActivity으로 클래스를 확장 할 수 ListView에 대한 안드로이드 ID를 사용하지 않는 경우.

당신이 ListActivity

android:id="@android:id/list 

에 의해 클래스를 확장하지만 android:id="@+id/dlist"를 사용하는 경우 다음

또한

당신이 사용 setContentView(R.layout.displaylist);을 보여 ID

를 가져 오는 전에 활동으로 클래스를 확장해야하는 경우 당신은 안드로이드 ID를 사용해야합니다

list = (ListView) findViewById(R.id.dlist); 
     list.setOnItemClickListener(itemclicklistener()); 
관련 문제