我在这段代码中想念的 [英] what i miss in this code

查看:88
本文介绍了我在这段代码中想念的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android中创建了一个简单的示例,可让您输入书名,书的作者和这本书的编号.我在点击事件上添加了按钮,这是主要活动的代码,用于向第二活动发送信息. br/> 这是第一个全班活动代码.

i created simple example in android that let you enter Name of book,author of the book and number of this book i add on click event to the button and this is the code for main activity to send info to second activity.
this is the first activity code whole class.

package com.test2;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;

public class Test2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    }
    public void OnAddClick(View button)
    {
    AutoCompleteTextView Name = (AutoCompleteTextView) findViewById(R.id.Name);
    AutoCompleteTextView Author = (AutoCompleteTextView) findViewById(R.id.Author);
    AutoCompleteTextView Number = (AutoCompleteTextView) findViewById(R.id.Number);

    
    Intent intent=new Intent();
	intent.setClass(this,Result.class);
    intent.putExtra("BookTitle", Name.getText().toString());
    intent.putExtra("AuthorName",Author.getText().toString());
    intent.putExtra("Number",Number.getText().toString());
    startActivity(intent);
    }
      public void OnCancelClick(View button)
    {
    	Intent intent=new Intent();
    	intent.setComponent(new ComponentName(this,Result.class));
    	intent.putExtra("Cancel", "Cancel");
    	
    	startActivity(intent);
    	
    	
    	
    }
    }




这是将重新获得意图的代码(整个类).




and this the code that will recived the intent (the whole class).

package com.test2;

import android.app.Activity;
import android.os.Bundle;

import android.widget.TextView;

public class Result extends Activity{
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    	setContentView(R.layout.result);
		TextView resultText=(TextView) findViewById(R.id.resultText);
		Bundle bundle=getIntent().getExtras();
		if(bundle.getString("Cancel")!=null)
		{
			resultText.setText("Operation Cancelled");
		}
		else
			
		{
		String BookTitle=bundle.getString("BookTitle");
		String BookAuthor=bundle.getString("AuthorName");
		String BookNumber=bundle.getString("Number");
		resultText.setText(
				"the result is"+ BookTitle + "-" + BookAuthor + "-" + BookNumber);
		}
	}
}


这个按钮的xml代码:


and this button xml code:

<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.11" android:text="Add" android:onClick="OnAddClick"></Button>








当我运行它给添加按钮和取消按钮给它意外的错误,它可以正常工作.我在代码中错过了什么?谢谢.








when i run it it give unexpected error for add button , and for cancel button it work normally. what i miss in the code ? thanks.

推荐答案


您能参加全班吗?

我认为,Intent变量未正确执行.
Hi,
Could you attatch full class.

I think ,the Intent variable is not wrok correctly.


这篇关于我在这段代码中想念的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆