2012-11-19 39 views
0

好的,所以我已經在這個幾個小時了,不能讓它工作。 基本上,我想從可點擊的列表視圖調用一個意圖。我展示的車型,它是名單上的代碼,當你點擊,你應該被帶到另一個屏幕上編輯了汽車的性能,但是,我beggining認爲這是不可能的Android自定義列表視圖點擊 - 如何調用意圖而不崩潰?

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, arrayListCarros); 
    mainListView.setAdapter(listAdapter);  
    db.close(); 

    mainListView.setClickable(true); 
    mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 

      Intent i = new Intent(listagemcarro.this,com.example.trabalhosql.edicao.class); 
      startActivity(i); 



     } 
    }); 

代碼的其餘部分:

public class listagemcarro extends Activity { 
Button buttonCadastro; 
Button buttonListagem; 
Button buttonBusca; 
Button button1; 

Intent itListagem = new Intent(); 
Intent itCadastro = new Intent(); 
Intent itBusca = new Intent(); 
//Intent itEdicao = new Intent(); 
Intent itEdicao2 = new Intent(); 

String id=""; 

public SQLiteDatabase db; 
public String BANCO = "banco.db"; 
public String TABELA = "carro"; 
int posicao=123123; 

private ListView mainListView ; 
private ArrayAdapter<String> listAdapter ; 

public void toast(int position){ 
    Context context = getApplicationContext(); 
    Toast toast = Toast.makeText(context, position, Toast.LENGTH_SHORT); 
    toast.show(); 

} 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listagemcarro); 

    Intent itRecebeParametros = getIntent(); 
    if(itRecebeParametros != null){ 
     id = itRecebeParametros.getStringExtra("id"); 
    } 


    db = openOrCreateDatabase(BANCO, Context.MODE_PRIVATE, null); 
    Cursor linhas = db.query(TABELA, new String[] {"ID_PESSOA, MODELO"},"id_pessoa = '"+id+"'", null, null, null, null); 

    mainListView = (ListView) findViewById(R.id.mainListView); 

    ArrayList <String>arrayListCarros = new ArrayList<String>(); 

    if(linhas.moveToFirst()){ 
     do{ 
      arrayListCarros.add(linhas.getString(0) +" " + linhas.getString(1)); 
     } 
    while(linhas.moveToNext()); 
    } 

    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, arrayListCarros); 
    mainListView.setAdapter(listAdapter);  
    db.close(); 

    mainListView.setClickable(true); 
    mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 

      Intent i = new Intent(listagemcarro.this,com.example.trabalhosql.edicao.class); 
      startActivity(i); 



     } 
    }); 

} 

錯誤

11-19 23:18:09.136: W/dalvikvm(1784): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
11-19 23:18:09.136: E/AndroidRuntime(1784): Uncaught handler: thread main exiting due to uncaught exception 
11-19 23:18:09.147: E/AndroidRuntime(1784): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trabalhosql/com.example.trabalhosql.edicao}: java.lang.NullPointerException 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 

11-19 23:18:09.147: E/AndroidRuntime(1784):  at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at android.os.Looper.loop(Looper.java:123) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at java.lang.reflect.Method.invoke(Method.java:521) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at dalvik.system.NativeStart.main(Native Method) 
11-19 23:18:09.147: E/AndroidRuntime(1784): Caused by: java.lang.NullPointerException 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at com.example.trabalhosql.edicao.onCreate(edicao.java:42) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
11-19 23:18:09.147: E/AndroidRuntime(1784):  ... 11 more 

edicao.class

package com.example.trabalhosql; 

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.ContentValues; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class edicao extends Activity { 

String id=""; 
String modeloCarro = ""; 
TextView textViewCarroEscolhido; 

public SQLiteDatabase db; 
public String BANCO = "banco.db"; 
public String TABELA = "carro"; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listagemcarro); 

    Intent itRecebeParametros = getIntent(); 
    if(itRecebeParametros != null){ 
     id = itRecebeParametros.getStringExtra("id"); 
     modeloCarro = itRecebeParametros.getStringExtra("modeloCarro"); 
    } 

    textViewCarroEscolhido = (TextView) findViewById(R.id.textViewCarroEscolhido); 
    textViewCarroEscolhido.setText(modeloCarro); 
} 

}

回答

0

編輯

我猜的edicao類的這部分是不正確:

setContentView(R.layout.listagemcarro); 

這本身可能沒有拋出的錯誤,而是試圖尋找意見(如你的textView),它不存在會。

+0

不起作用,它們基本上是等效的代碼。 – tehort

+1

你可以發佈'edicao.class'的OnCreate嗎?第42行是什麼?那裏似乎有些東西。 – mango

+0

好吧,我用代碼編輯了我的文章,但我不認爲edicao.class是一個問題,我可以通過一個普通的按鈕來完美地調用它。 – tehort

0

textViewCarroEscolhido爲null。確保它存在於listagemcarro.xml中

0

如果有人來這裏調用畫布等繪畫場景中的意圖。確保你沒有多次打電話給這個意圖。在我的情況下,invalidate()方法不斷被調用,並進行了許多意向調用。確保使用某種翻轉機制來阻止invalidate()被呼叫。會像魅力一樣工作。

相關問題