Android活动 - 无法实例化活动组件信息

时间:2014-03-06 03:31:22

标签: android-intent

我正在尝试编写一个程序,在单击下一个按钮时显示一个字符串“hello”。 我参加了两项活动。当我跑步时,我得到了这个日志猫。我是Android编程新手,请帮帮我。

我的主要活动是,

public class MainActivity extends Activity {

    public final static String EXTRA_MESSAGE = "com.example.trailcarehand.MESSAGE";
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    Button button =(Button)findViewById(R.id.btnNext);

    public void onClick(View view)

{
Intent i=new Intent(this,DisplayMessageActivity.class);

        String message = "hello";
        i.putExtra(EXTRA_MESSAGE, message);
        startActivity(i);

        //count++;

      }
}

我的displaymessage活动是

 public class DisplayMessageActivity extends Activity{

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


        // Get the message from the intent
        Intent i = getIntent();
        String message = i.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = (TextView) findViewById(R.id.textView1);
        textView.setTextSize(40);
        textView.setText(message);


        // Set the text view as the activity layout
        setContentView(R.layout.activity_main);

    }

我的activity_main.xml是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="26dp"
    android:paddingBottom="5dp"
    android:text=" welcome "
    android:textColor="#f7f9f6"
    android:textSize="13dp"
    android:textStyle="italic" />


<Button android:text="Next"
 android:id="@+id/btnNext"
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
 android:layout_below="@id/textView1"
 android:onClick="onClick"/> 


</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您的代码看起来正确,我想您忘记在AndroidManifest.xml文件中声明您的活动。

<activity android:name="com.your.package.DisplayMessageActivity"></activity>

答案 1 :(得分:0)

你好请做出改变

您的代码

public class MainActivity extends Activity {

public final static String EXTRA_MESSAGE = "com.example.trailcarehand.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
**setContentView(R.layout.activity_main);
}
Button button =(Button)findViewById(R.id.btnNext);
public void onClick(View view)**
  {
Intent i=new Intent(this,DisplayMessageActivity.class);

    String message = "hello";
    i.putExtra(EXTRA_MESSAGE, message);
    startActivity(i);

    //count++;

  }
}

更改代码

public class MainActivity extends Activity **implement onClickListner**{

public final static String EXTRA_MESSAGE = "com.example.trailcarehand.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button =(Button)findViewById(R.id.btnNext);
}
@override
public void onClick(View view)
 {
   Intent i=new Intent(this,DisplayMessageActivity.class);

    String message = "hello";
    i.putExtra(EXTRA_MESSAGE, message);
    startActivity(i);

    //count++;
  }
 }
如果你没有实现,

实现onclicklistner。