【问题标题】:intent not working意图不工作
【发布时间】:2011-02-20 03:43:13
【问题描述】:

我正在学习Intents 的概念,当我尝试从一个意图转到另一个意图时,我正在处理的应用程序崩溃,意图对象在按钮的onClickListener() 上被触发。

下面是代码:

intentdemo.java:第一个活动

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class intentdemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Button btn1= (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent i = new Intent(intentdemo.this,Intentdemo2.class);
                startActivity(i);
            }

       });




    }
}

Intentdemo2.java:活动二

package com.test.intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Intentdemo2 extends Activity {
    @Override

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button)findViewById(R.id.button2);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getBaseContext(),intentdemo.class);
                startActivity(i);
            }
        });

    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.test.intent"
  android:versionCode="1"
  android:versionName="1.0">


<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".intentdemo"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Intentdemo2"></activity>
</application>
</manifest>

main.xml:用于第一个活动

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button 
    android:text="Button" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </Button>
</LinearLayout>

layout.xml:用于第二个活动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <Button android:id="@+id/button2" android:layout_height="wrap_content" android:text="Button" android:layout_width="wrap_content"></Button>
    <TextView 
    android:text="@string/text" 
    android:id="@+id/textView1" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></TextView>
</LinearLayout>

如果有人能在这里指出我的错误,我将不胜感激,首先显示活动,当我单击按钮时,它会给我消息“活动已意外停止”,如果活动中的错误是,我应该如何调试不直观。

谢谢, 席德

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    您的第二个活动正在从 main.xml (setContentView(R.layout.main);) 加载其布局,但正在尝试查找 ID 为 R.id.button2 的按钮。主布局中没有这样的按钮,因此尝试设置点击侦听器会在尝试访问 null 对象时崩溃。

    解决方法很简单 - 将行更改为 setContentView(R.layout.layout);。 (另外说明 - 将该文件的名称从 layout.xml 更改为其他名称)

    【讨论】:

    • 这行得通,谢谢 Franci,是的,我会记住将文件名从 layout.xml 更改为其他名称。
    【解决方案2】:

    我只是在这里猜测,但不应该

        setContentView(R.layout.main);
    

        setContentView(R.layout.layout);
    

    在 IntentDemo2 中

    【讨论】:

      【解决方案3】:

      我认为你刚刚复制并粘贴了第二个主要活动...所以请将setContentView(R.layout.main); 更改为适当的布局。

      你的第一个活动,

      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      
      
      public class intentdemo extends Activity {
          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
      
      
              Button btn1= (Button) findViewById(R.id.button1);
              btn1.setOnClickListener(new OnClickListener() {
      
                  public void onClick(View v) {
      
                      Intent i = new Intent(intentdemo.this,Intentdemo2.class);
                      startActivity(i);
                  }
      
             });
      
      
      
      
          }
      }
      

      你的第二个活动,

      package com.test.intent;
      
      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      
      public class Intentdemo2 extends Activity {
          @Override
      
          public void onCreate(Bundle savedInstanceState)
          {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.second);
              Button btn = (Button)findViewById(R.id.button2);
              btn.setOnClickListener(new OnClickListener() {
      
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
      
                  }
              });
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-01
        • 2013-01-28
        • 1970-01-01
        相关资源
        最近更新 更多