0

好的,所以我已经在这上面做了几个小时,但无法让它工作。基本上,我想从可点击的列表视图中调用一个意图。我正在显示汽车型号,它是列表中的代码,当您单击时,您应该被带到另一个屏幕来编辑汽车的属性,但是,我开始认为这是不可能的

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);
}

}

4

3 回答 3

0

编辑

我猜 edicao 课程的这一部分是不正确的:

setContentView(R.layout.listagemcarro);

这本身可能不会引发错误,但尝试在其中查找不存在的视图(如您的 textView)会。

于 2012-11-19T23:32:07.723 回答
0

textViewCarroEscolhido 为空。确保它存在于 listagemcarro.xml

于 2012-11-19T23:47:27.713 回答
0

万一有人来这里是为了在画布等绘图情况下调用意图。确保您没有多次调用意图。在我的情况下,该invalidate()方法不断被调用并进行了许多意图调用。确保使用某种翻转机制来停止invalidate()被调用。会像魅力一样工作。

于 2017-04-23T21:26:18.127 回答