【问题标题】:Can't do a simple data passing with an intent (Android)无法进行有意图的简单数据传递(Android)
【发布时间】:2015-11-23 16:05:49
【问题描述】:

它在其他活动中显示 xml,我的意思是,意图有效,但我的字符串值不符合预期。

我只想让其他活动中的字符串在画布中用颜色填充图形。画布使用局部变量。

我想得到我其他活动的F*cking字符串值,我的意思是,我在意图字符串值上加上了一个“valor”,仍然不起作用,不知道为什么。

非常感谢任何帮助,我猜问题出在 getIntent 方法或类似方法中。

接收者活动

package com.example.escom.dbms2;

import android.content.Context;
import android.content.Intent;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.Rect;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;

    public class FiguraActvity extends AppCompatActivity {

public final static String KEYFIG = "colorID";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = getIntent().getExtras();
    String cID = (String) bundle.get(KEYFIG);

    Figura currentFig = new Figura(this);


    currentFig.setCurrentColorID(cID);

    if (cID =="valor") {
        setContentView(currentFig);
    }

    setContentView(R.layout.activity_figura_actvity);

}



public class Figura extends View {

    String currentColorID;

    public Figura(Context c) {
        super(c);



    }

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);




        Rect rectangulo = new Rect();
        rectangulo.set(0,0,canvas.getWidth()/2,canvas.getHeight()/2);

        Paint currentcolor = new Paint();

        if (currentColorID =="1") {
            currentcolor.setColor(Color.WHITE);
        }

        else if (currentColorID =="2"){
            currentcolor.setColor(Color.BLUE);
        }

        else if (currentColorID =="3"){
            currentcolor.setColor(Color.RED);
        }

        else  {
            currentcolor.setColor(Color.YELLOW);
        }

        currentcolor.setStyle(Paint.Style.FILL);

        canvas.drawRect(rectangulo,currentcolor);




    }

    public void setCurrentColorID(String ID){

        currentColorID=ID;

    }


}

    }

     package com.example.escom.dbms2;


    import android.app.Activity;
    import android.app.AlertDialog.Builder;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.*;

Sender Activity,在 if(v==jbnV) 是intent。

    public class DbmsActivity2 extends Activity implements OnClickListener{

public final static String KEYFIG = "colorID";
EditText        jetI, jetN;
Button          jbnA, jbnB, jbnC, jbnV, jbnL, jbnI, jbnF;
SQLiteDatabase  db;
String currentColorID;
String currentColorName;
@Override
public void onCreate(Bundle b){
    super.onCreate(b);
    setContentView(R.layout.activity_dbms2);
    jetI=(EditText)findViewById(R.id.xetI);
    jetN=(EditText)findViewById(R.id.xetN);

    jbnA=(Button)findViewById(R.id.xbnA);   jbnA.setOnClickListener(this);
    jbnB=(Button)findViewById(R.id.xbnB);   jbnB.setOnClickListener(this);
    jbnC=(Button)findViewById(R.id.xbnC);   jbnC.setOnClickListener(this);
    jbnV=(Button)findViewById(R.id.xbnV);   jbnV.setOnClickListener(this);
    jbnL=(Button)findViewById(R.id.xbnL);   jbnL.setOnClickListener(this);
    jbnI=(Button)findViewById(R.id.xbnI);   jbnI.setOnClickListener(this);
    jbnF=(Button)findViewById(R.id.xbnF);   jbnF.setOnClickListener(this);



    db=openOrCreateDatabase("DBColores", Context.MODE_PRIVATE, null);

    db.execSQL("CREATE TABLE IF NOT EXISTS colores (ID VARCHAR, nombre VARCHAR);");

    db.execSQL("DELETE FROM colores WHERE ID='" + "1" + "'");
    db.execSQL("DELETE FROM colores WHERE ID='" + "2" + "'");
    db.execSQL("DELETE FROM colores WHERE ID='" + "3" + "'");
    db.execSQL("DELETE FROM colores WHERE ID='" + "4" + "'");
    db.execSQL("INSERT INTO colores VALUES('" + "1" + "','" + "BLACK" + "');");
    db.execSQL("INSERT INTO colores VALUES('" + "2" + "','" + "BLUE" + "');");
    db.execSQL("INSERT INTO colores VALUES('" + "3" + "','" + "RED" + "');");
    db.execSQL("INSERT INTO colores VALUES('" + "4" + "','" + "YELLOW" + "');");

}
public void onClick(View v){
    if(v==jbnA){
        if( jetI.getText().toString().trim().length()==0 || jetN.getText().toString().trim().length()==0){
            mensaje("Error", "Ingresar todos los datos");
            return;
        }
        db.execSQL("INSERT INTO colores VALUES('" + jetI.getText() + "','" + jetN.getText() + "');");
        mensaje("Alta", "Registro agregado");
        limpiar();
    }
    if(v==jbnB){
        if(jetI.getText().toString().trim().length()==0){
            mensaje("Error", "Ingresar el ID");
            return;
        }
        Cursor c=db.rawQuery("SELECT * FROM colores WHERE ID='" + jetI.getText() + "'", null);
        if(c.moveToFirst()) {
            db.execSQL("DELETE FROM colores WHERE ID='" + jetI.getText() + "'");
            mensaje("Baja", "Registro eliminado");
        } else {
            mensaje("Error", "ID inválido");
        }
        limpiar();
    }
    if(v==jbnC){
        if(jetI.getText().toString().trim().length()==0) {
            mensaje("Error", "Ingresar el ID");
            return;
        }
        Cursor c=db.rawQuery("SELECT * FROM colores WHERE ID='" + jetI.getText() + "'", null);
        if(c.moveToFirst()) {
            db.execSQL("UPDATE colores SET nombre='" + jetN.getText()  + "' WHERE ID='" + jetI.getText() + "'");
            mensaje("Cambio", "Registro modificado");
        } else {
            mensaje("Error", "ID inválido");
        }
        limpiar();
    }
    if(v==jbnV) {
        if(jetI.getText().toString().trim().length()==0) {
            mensaje("Error", "Ingresar el ID");
            return;
        }

        Cursor c=db.rawQuery("SELECT * FROM colores WHERE ID='" + jetI.getText() + "'", null);
        if(c.moveToFirst()) {

           //currentColorID = jetI.getText().toString();
            currentColorID = "1";


           Intent i;
            i = new Intent(getApplicationContext(), FiguraActvity.class);


            i.putExtra(KEYFIG,"valor");


            startActivity(i);

        }
        else   {
            mensaje("Error", "ID inválido");
            limpiar();
        }
    }
    if(v==jbnL)  {
        Cursor c=db.rawQuery("SELECT * FROM colores", null);
        if(c.getCount()==0){
            mensaje("Error", "No hay registros");
            return;
        }
        StringBuffer buffer=new StringBuffer();
        while(c.moveToNext())    {
            buffer.append("ID: " + c.getString(0)+"\n");
            buffer.append("Nombre: " + c.getString(1)+"\n");

        }
        mensaje("Lista", buffer.toString());
    }
    if(v==jbnI)  {
        mensaje("Examen App Development for Mobile", "ESCOM");
    }


    if(v==jbnF)  {

    startActivity(new Intent(DbmsActivity2.this, LienzoActivity.class));

    }

}
public void mensaje(String s,String m)  {
    Builder b=new Builder(this);
    b.setCancelable(true);
    b.setTitle(s);
    b.setMessage(m);
    b.show();
}
public void limpiar()    {
    jetI.setText("");
    jetN.setText("");
    jetI.requestFocus();
}






 }

重要的发件人活动行

           currentColorID = "1";


           Intent i;
            i = new Intent(getApplicationContext(), FiguraActvity.class);


            i.putExtra(KEYFIG,"valor");


            startActivity(i);

重要的接收者活动线

       @Override
       protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    Bundle bundle = getIntent().getExtras();
    String cID = (String) bundle.get(KEYFIG);

两个活动中的变量

   public final static String KEYFIG = "colorID";

XML 清单

http://pastebin.com/2nwpCGkC

【问题讨论】:

  • cID =="valor" 使它成为 cID.equals("valor") 。你能验证一下吗让我知道
  • 它做到了,我不认为这是问题。
  • 你需要保留 setContentView(R.layout.activity_figura_actvity);在其他情况下。张贴在我的答案中。你能验证一下吗
  • 有什么更新吗?如果你已经修好了。请发布答案以确保我们已关闭此线程。
  • 是的,Ramesh,你是对的。这是平等的,加上 setContent 视图,非常感谢...顺便说一句,有没有办法支持正确的答案?

标签: java android xml


【解决方案1】:

我认为您在传递值方面没有问题。比较时,您是在与 == 进行比较,但您应该与 equals 进行比较。

if (cID.equals("valor")) {
    setContentView(currentFig);
} else {
    setContentView(R.layout.activity_figura_actvity);
} 

【讨论】:

    【解决方案2】:

    您可以做的是从意图代码中提取值(根据其类型)的位置应为:

    String cID = getIntent().getStringExtra(KEYFIG);

    您想要将值置于意图中的位置,它应该是

    i.putExtra(KEYFIG,"valor");

    我想这会成功

    【讨论】:

    • 我已经尝试过了,我认为 Intent 或 get Intent 有问题。
    【解决方案3】:

    错误在这里:

    i.putExtra(KEYFIG,"valor");
    

    因为你已经直接把价值作为额外的。但是,在提取或接收值时,您首先提取捆绑包,然后从捆绑包中提取值。

    所以,要么将额外值作为捆绑包放在发送方,然后它就会起作用。 否则,只需在接收方获得额外费用。

    另外, 问题可能在于比较值,因为您使用的是“==”,它用于比较引用而不是值。您应该使用 equals() 来比较值。

    【讨论】:

    • putExtra 也插入到 mExtras 中,这是一个捆绑包。所以当他说 getExtras 时,它会返回相同的包。我不认为这是一个错误。
    • if (cID =="valor") { setContentView(currentFig); } // "==" 用于比较引用或内存地址。 equals() 应该用于匹配值。这可能是错误。
    • 是的,这就是我已经发布的答案! putExtra 只是处理包的包装器。
    • 我已经尝试在 Intent 和 Bundle 中使用 putExtra(String)。
    • 把“==”换成等号然后试试
    猜你喜欢
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    相关资源
    最近更新 更多