2014-02-12 125 views
0

我嘗試獲取LinearLayout中的數據,該數據本身位於ListView中。 所以,在我的邏輯中,我把一個ID放在LinearLayout上,我加了一個OnToucheListener。 但是,當我運行應用程序應用程序意外退出。ListView中LinearLayout上的事件

我想獲得TexTview這是在LinearLayout,以便允許更改數據在AlertDialog

我實現了我的ListView在我AsyncTaskonPostExecute()一個SimpleAdapter,所以到現在爲止,所有的作品,但我不知道如何獲得的LinearLayout誰是我的ListView的元素(TextView的)..

因此,這是我的代碼嘗試使用AlertDialog獲取linearLayout以允許更改數據。

LayoutInflater factory = LayoutInflater.from(this); 
    final View alertDialogView = factory.inflate(R.layout.alert_dialog_divers, null); 


    LinearLayout linear = (LinearLayout) findViewById(R.id.test); 
    linear.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      AlertDialog.Builder adb = new AlertDialog.Builder(listview.this);  
      adb.setView(alertDialogView); 
      adb.setTitle("Divers");    
      adb.setIcon(android.R.drawable.ic_dialog_alert);    
      adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int whichButton) { 

        EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1); 

       } 
      }); 

      adb.setNegativeButton("Annuler", null); 
      //on affiche la boite de dialogue 
      adb.show(); 
     } 
    }); 

而這是我的listView xml文件。 (我在紅色的寵物更好地看到哪裏是我的LinearLayout)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:baselineAligned="false" 
android:orientation="horizontal" > 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="230dp" 
    android:layout_weight="0.56" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:background="@drawable/border" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/collector_id" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:text="> B10954-1" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/adress" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="10dp" 
      android:layout_weight="0" 
      android:text="AIDER BOURGOGNE 14 Rue de la Breuchillière 21000 DIJON " 
      android:textSize="20sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="80dp" 
      android:layout_marginTop="5dp" 
      android:text="@string/depose" 
      android:textSize="25sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/product_code" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:padding="5dp" 
      android:text="3 F50D - 2 C50HD - 3 F50D - 2 C50HD - 4 BA300 - 5CE2 - 4F30E" 
      android:textSize="15sp" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/border" 
     android:baselineAligned="false" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1.75" 
      android:background="@drawable/border" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/border" > 

       <TextView 
        android:id="@+id/begin_hour" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0" 
        android:paddingLeft="5dp" 
        android:text="07:30-12:30 " 
        android:textSize="20sp" 
        android:textStyle="bold" /> 

       <TextView 
        android:id="@+id/end_hour" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:paddingLeft="5dp" 
        android:text="14:00-19:00" 
        android:textSize="20sp" 
        android:textStyle="bold" /> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"      
       android:orientation="vertical" > 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" > 

        <TextView 
         android:id="@+id/textView1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_margin="5dp" 
         android:text="@string/ordre" 
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:textSize="25sp" 
         android:textStyle="bold" /> 

        <TextView 
         android:id="@+id/order" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_margin="5dp" 
         android:text="2" 
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:textSize="20sp" /> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" > 

        <TextView 
         android:id="@+id/text" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_margin="5dp" 
         android:text="@string/tpsarret" 
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:textSize="25sp" 
         android:textStyle="bold" /> 

        <TextView 
         android:id="@+id/downtime" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_margin="5dp" 
         android:text="2 min" 
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:textSize="20sp" /> 

       </LinearLayout> 
      </LinearLayout> 
     </LinearLayout> 

Below, the linearLyout in question with id : test --------------------------------- 

     <LinearLayout 
      android:id="@+id/test" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1.5" 
      android:background="@drawable/linear_option" 
      android:clickable="true"    
      android:orientation="vertical" > 
      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/border" 
       android:weightSum="2" > 
       <TextView 
        android:id="@+id/fax" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:paddingLeft="5dp" 
        android:text="03 80 91 03 03 " 
        android:textSize="20sp" 
        android:textStyle="bold" /> 
       <TextView 
        android:id="@+id/tel" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text=" 06 43 66 53 16" 
        android:textSize="20sp" 
        android:textStyle="bold" /> 
      </LinearLayout> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 
       <ImageView 
        android:id="@+id/imageView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/crayon_icon" /> 
       <TextView 
        android:id="@+id/textView11" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="@string/divers" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:textSize="25sp" 
        android:textStyle="bold" /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/miscellaneous" 
       android:layout_width="333dp" 
       android:layout_height="fill_parent" 
       android:layout_marginLeft="5dp" 
       android:paddingLeft="40dp" 
       android:textSize="20sp" /> 
     </LinearLayout> 

End of the linearLayout in question ---------------------------------------------------- 

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1.5" 
      android:background="@drawable/linear_option" 
      android:clickable="true" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/textView6" 
       android:layout_width="fill_parent" 
       android:layout_height="28dp" 
       android:background="@drawable/border" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

       <ImageView 
        android:id="@+id/imageView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/crayon_icon" /> 

       <TextView 
        android:id="@+id/textView12" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="@string/infosco" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:textSize="25sp" 
        android:textStyle="bold" /> 

      </LinearLayout> 

      <TextView 
       android:id="@+id/infos" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_marginLeft="5dp" 
       android:paddingLeft="40dp" 
       android:textSize="20sp" /> 

     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="118dp" 
    android:layout_height="230dp" 
    android:background="@drawable/border" 
    android:orientation="vertical" 
    android:descendantFocusability="blocksDescendants"> 

    <FrameLayout 
     android:layout_width="66dp" 
     android:layout_height="73dp" 
     android:layout_gravity="center" 
     android:layout_marginTop="50dp" 
     android:background="@drawable/editer_doc" > 

    </FrameLayout> 

</LinearLayout> 

正如你可以看到,有很多LinearLayout父。所以當我用id = test觸摸佈局時,我想要得到帶有id的textView:miscellaneous並允許改變它。

我希望你能理解。

EDIT 1:

02-12 08:45:54.370: E/AndroidRuntime(9076): FATAL EXCEPTION: main 
02-12 08:45:54.370: E/AndroidRuntime(9076): java.lang.RuntimeException: Unable to start    activity ComponentInfo{com.main/com.main.listview}: java.lang.NullPointerException 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at android.app.ActivityThread.access$600(ActivityThread.java:123) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at android.os.Looper.loop(Looper.java:137) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at dalvik.system.NativeStart.main(Native Method) 
02-12 08:45:54.370: E/AndroidRuntime(9076): Caused by: java.lang.NullPointerException 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at com.main.listview.onCreate(listview.java:68) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at android.app.Activity.performCreate(Activity.java:4465) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
02-12 08:45:54.370: E/AndroidRuntime(9076):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 

編輯2:

public class listview extends Activity { 

private ListView maListViewPerso; 
ImageButton imgButton; 
Boolean isConnected; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ActionBar actionBar = getActionBar(); 
    actionBar.setBackgroundDrawable(new ColorDrawable(Color 
      .parseColor("#000000"))); 

    maListViewPerso = (ListView) findViewById(R.id.listviewperso); 
    isConnected = isOnline(); 

    getData(isConnected); 

    //On instancie notre layout en tant que View 
    LayoutInflater factory = LayoutInflater.from(this); 
    final View alertDialogView = factory.inflate(R.layout.alert_dialog_divers, null); 


    LinearLayout linear = (LinearLayout) findViewById(R.id.test); 
    linear.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      AlertDialog.Builder adb = new AlertDialog.Builder(listview.this);  
      adb.setView(alertDialogView); 
      adb.setTitle("Divers");    
      adb.setIcon(android.R.drawable.ic_dialog_alert);    
      adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int whichButton) { 

        EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1); 

       } 
      }); 

      adb.setNegativeButton("Annuler", null); 
      //on affiche la boite de dialogue 
      adb.show(); 
     } 
    }); 

我listeView負載whend的getData()被調用。 (此函數調用的AsyncTask,並填寫一個HashMap誰填補ListView控件與SimpleAdapter)

編輯3:

package com.main; 


import android.app.ActionBar; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.res.AssetManager; 
import android.graphics.Color; 
import android.graphics.drawable.ColorDrawable; 
import android.widget.AdapterView; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.io.StringWriter; 
import java.io.UnsupportedEncodingException; 
import java.io.Writer; 
import java.util.ArrayList; 
import java.util.HashMap; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import com.pdf.GetViewPDF; 

public class listview extends Activity { 

private ListView maListViewPerso; 
ImageButton imgButton; 
Boolean isConnected; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ActionBar actionBar = getActionBar(); 
    actionBar.setBackgroundDrawable(new ColorDrawable(Color 
      .parseColor("#000000"))); 

    maListViewPerso = (ListView) findViewById(R.id.listviewperso); 
    isConnected = isOnline(); 

    getData(isConnected); 
    setdivers(); 




} 

public void setdivers(){ 
    //On instancie notre layout en tant que View 
    LayoutInflater factory = LayoutInflater.from(this); 
    final View alertDialogView = factory.inflate(R.layout.alert_dialog_divers, null); 


    LinearLayout layout = (LinearLayout) findViewById(R.id.test); 
    layout.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      AlertDialog.Builder adb = new AlertDialog.Builder(listview.this);  
      adb.setView(alertDialogView); 
      adb.setTitle("Divers");    
      adb.setIcon(android.R.drawable.ic_dialog_alert);    
      adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int whichButton) { 

        EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1); 

       } 
      }); 

      adb.setNegativeButton("Annuler", null); 
      //on affiche la boite de dialogue 
      adb.show(); 
     } 
    }); 
} 

public boolean isOnline() { 
    ConnectivityManager cm = 
      (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
     return true; 
    } 
    return false; 
} 



@Override 
public void onBackPressed() { 
    Toast.makeText(this,"Action Désactivée", Toast.LENGTH_LONG).show(); 
    //    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); 
    //    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 
} 

public void GoToPDF(){ 
    Intent intent = new Intent(this, GetViewPDF.class); 
    startActivity(intent);   
} 
public void getData(Boolean isConnected){ 

    if(isConnected){ 
     asyncTask asynchrone_task_tournee = new asyncTask(listview.this, maListViewPerso); 
     asynchrone_task_tournee.execute(); 

    }else{ 
     //J'initialise toutes les variables 
     JSONArray jArray = null; 
     JSONObject json_data=null;  
     HashMap<String, String> map; 
     ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();   
     InputStream is = null;  
     Writer writer = new StringWriter(); 
     char[] buffer = new char[1024]; 

     //je récupère le fichier .txt de donner JSON dans le repertoir asset 
     AssetManager am = listview.this.getAssets(); 
     try { 
      is = am.open("donnees_tourne.txt"); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 


     //Je lis le fichier .txt 
     try { 
       Reader reader = null; 
       int n; 
       try { 
        reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 
        while ((n = reader.read(buffer)) != -1) 
        { 
         writer.write(buffer, 0, n); 
        } 


       } catch (UnsupportedEncodingException e) {     
        e.printStackTrace(); 
       } catch (IOException e) {     
        e.printStackTrace(); 
       } 

      } finally { 
       try { 
        is.close(); 
       } catch (IOException e) {     
        e.printStackTrace(); 
       } 
      } 
     //recuperation des donnees json dans jsonString 
     String jsonString = writer.toString(); 

     Log.i("JSON_NOT_connected",jsonString); 

     //traitement du json dans le tableau json JSONArray 
     try { 
      jArray = new JSONArray(jsonString); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }   

     //Insertion des données json dans un Hashmap 
     for(int i=0;i < jArray.length();i++) 
     {    
      try { 

       json_data = jArray.getJSONObject(i); 

       map = new HashMap<String, String>(); 
       map.put("collector_id", json_data.getString("collector_id")); 
       map.put("adress", json_data.getString("adress")); 
       map.put("product_code", json_data.getString("product_code")); 
       map.put("begin_hour", json_data.getString("begin_hour")); 
       map.put("end_hour", json_data.getString("end_hour")); 
       map.put("order", json_data.getString("order")); 
       map.put("downtime", json_data.getString("downtime")); 
       map.put("fax", json_data.getString("fax")); 
       map.put("tel", json_data.getString("tel")); 
       map.put("miscellaneous", json_data.getString("miscellaneous")); 
       map.put("infos", json_data.getString("infos")); 

       listItem.add(map); 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }  
     } 

     SimpleAdapter mSchedule = new SimpleAdapter (listview.this, listItem, R.layout.list_type_tournee, 
       //noms donnés en clé dans le Hashmap 
       new String[] {"collector_id", "adress","product_code","begin_hour", 
           "end_hour","order","downtime","fax", 
           "tel","miscellaneous","infos"}, 
       //variables textview dans la list_type_tournee.xml    
       new int[] {R.id.collector_id, R.id.adress,R.id.product_code,R.id.begin_hour, 
          R.id.end_hour,R.id.order,R.id.downtime,R.id.fax, 
          R.id.tel,R.id.miscellaneous,R.id.infos}); 

     maListViewPerso.setAdapter(mSchedule); 



    } 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.point_menu, menu); 
    menu.getItem(0).setTitle(UtilClass.stringNow()); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.menu_now: 
     return true; 
    case R.id.menu_login: 
     Intent activiteU = new Intent(listview.this, LoginActivity.class); 
     startActivity(activiteU); 
     return true; 
    case R.id.menu_tour_inf: 
     Intent activiteT = new Intent(listview.this, TourActivity.class); 
     activiteT.putExtra("userid", 10); 
     startActivity(activiteT); 
     return true; 
    case R.id.menu_point_list: 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

}

+0

使用簡單的佈局結構..這包括幾乎4個正常的佈局 –

+0

我做了這個結構,因爲我有責任,我應該有這樣的演示 –

回答

0

我是正確的理解NullPointerException異常是在這條線?

LinearLayout linear = (LinearLayout) findViewById(R.id.test); 

它看起來像什麼findViewById被隱式調用的東西,沒有任何您發佈的佈局,或佈局已加載之前被調用。 你可以發佈其他代碼嗎?

+0

我把其餘的代碼onCreate(),編輯2 。我沒有放我的AsyncTask,因爲我認爲這並不重要,因爲它只是填充listView。所以非常規,就像我之前調用getData()一樣,應該加載帶有id:Test的LinearLayout –

+0

你能告訴我第68行是什麼嗎?你發佈的代碼只有48行,但可能有導入和類聲明前的包名稱 – nyx

+0

是的lin哪個塊,是我做的時候:linear.setOnClickListener(new View.OnClickListener()... –