简体   繁体   English

安卓(意图)

[英]Android ( Intent )

i have these peace of code taht is written by eclipse and which gives me no syntax error but when i run it , it says intent_ has stopped working我有这些和平的代码 taht 是由 eclipse 编写的,它没有给我任何语法错误,但是当我运行它时,它说 intent_ 已停止工作

Here is my code:这是我的代码:

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;
    }

}

Please help me solve the problem Thank you !请帮我解决问题谢谢!

My guess is that b2 wasn't defined.我的猜测是b2没有定义。
so b2.setOnClickListener will give you a null pointer error.所以b2.setOnClickListener会给你一个空指针错误。

Further to Saba's answer - you have this twice进一步 Saba 的回答 - 你有两次

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

The second one should probably be b2 = ...第二个应该是 b2 = ...

You haven't any button b2 for b2.setOnClickListener你没有 b2.setOnClickListener 的任何按钮 b2

//running code is below //运行代码如下

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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM