【问题标题】:Android ( Intent )安卓(意图)
【发布时间】:2014-04-07 20:42:25
【问题描述】:

我有这些代码是由 Eclipse 编写的,没有语法错误 但是当我运行它时,它说intent_已停止工作

这是我的代码:

package com.example.intent_;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    TextView t1, t2, t3;
    EditText tf1;
    Button b1, b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toast.makeText(MainActivity.this, "Welcome to On create", Toast.LENGTH_LONG).show();
        b1 = (Button) findViewById(R.id.button1);
        t1 = (TextView) findViewById(R.id.textView1);
        t2 = (TextView) findViewById(R.id.textView2);
        t3 = (TextView) findViewById(R.id.textView3);
        b1 = (Button) findViewById(R.id.button1);
        tf1 = (EditText) findViewById(R.id.editText1);
        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                t1.setText("Abed-Almalik");
                t2.setText("Jorda-jerash");
                t3.setText("2010901092");
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String url = "http://www.google.com";
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

请帮我解决问题 谢谢!

【问题讨论】:

  • 你的包名com.example.intent_应该是com.example.intent。要查看错误详细信息,请查看 logcat 并将其包含在问题中。
  • 除了@donfuxx 说的,你能说说问题标签打印了什么吗?
  • intent_ 是我的项目名称,所以这里没问题
  • 那么请发布 logcat 输出 ;-)

标签: java android android-intent


【解决方案1】:

我的猜测是 b2 没有定义。
所以b2.setOnClickListener 会给你一个空指针错误。

【讨论】:

    【解决方案2】:

    Saba 的进一步回答 - 你有这个两次

    b1 = (Button) findViewById(R.id.button1);
    

    第二个应该是 b2 = ...

    【讨论】:

      【解决方案3】:

      b2.setOnClickListener 没有任何按钮 b2

      //运行代码如下

      package com.example.intent_;
      
      import android.app.Activity;
      import android.content.Intent;
      import android.net.Uri;
      import android.os.Bundle;
      import android.view.Menu;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.TextView;
      import android.widget.Toast;
      
      public class MainActivity extends Activity {
      
          TextView t1, t2, t3;
          EditText tf1;
          Button b1, b2;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              Toast.makeText(MainActivity.this, "Welcome to On create", Toast.LENGTH_LONG).show();
              b1 = (Button) findViewById(R.id.button1);
              t1 = (TextView) findViewById(R.id.textView1);
              t2 = (TextView) findViewById(R.id.textView2);
              t3 = (TextView) findViewById(R.id.textView3);
              b2 = (Button) findViewById(R.id.button2);  //here change it to b2 and button2
              tf1 = (EditText) findViewById(R.id.editText1);
              b1.setOnClickListener(new View.OnClickListener() {
      
                  @Override
                  public void onClick(View arg0) {
                      // TODO Auto-generated method stub
                      t1.setText("Abed-Almalik");
                      t2.setText("Jorda-jerash");
                      t3.setText("2010901092");
                  }
              });
      
              b2.setOnClickListener(new View.OnClickListener() {
      
                  @Override
                  public void onClick(View arg0) {
                      // TODO Auto-generated method stub
                      String url = "http://www.google.com";
                      Intent i = new Intent(Intent.ACTION_VIEW);
                      i.setData(Uri.parse(url));
                      startActivity(i);
      
                  }
              });
      
          }
      
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
          }
      
      }
      

      【讨论】:

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