Android应用在启动新活动时崩溃

时间:2016-11-26 15:48:21

标签: android

我是开发初学者(我的第一个项目)请帮忙!当我启动一个名为fromdkk的新活动时,通过按下一个没有任何功能的按钮从activity_main中选择它可以工作,但是当我尝试在该新活动上实际做某事时,例如运行init();它会立即停止。我应该改变什么?

...进口

公共类fromdkk扩展AppCompatActivity实现了View.OnClickListener {

private Button USD,CAD,EUR,GBP;
private EditText insert;
private TextView result;

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

private void init() {

    USD=(Button)findViewById(R.id.usd);
    CAD=(Button)findViewById(R.id.cad);
    EUR=(Button)findViewById(R.id.eur);
    GBP=(Button)findViewById(R.id.gbp);


    insert=(EditText)findViewById(R.id.ins);
    result=(TextView)findViewById(R.id.res);



    USD.setOnClickListener(this);// on click do this which is stated later as a function
    EUR.setOnClickListener(this);
    CAD.setOnClickListener(this);
    GBP.setOnClickListener(this);

}
@Override
public void onClick(View view) {
    String num1=insert.getText().toString();//get text from user
    float usd1= (float) 0.142;
    float eur= (float) 0.134;
    float cad= (float) 0.192;
    float gbp= (float) 0.114;


    switch(view.getId()) {//switch between an cases regarding Ids, execute operation which matches button id


        case R.id.usd:
            float curr = Float.parseFloat(num1) * Float.parseFloat(String.valueOf(usd1));
            result.setText(String.valueOf(curr) + " $");
            break;

        case R.id.cad:
            float curr2 = Float.parseFloat(num1) * Float.parseFloat(String.valueOf(cad));
            result.setText(String.valueOf(curr2) + " $");
            break;

        case R.id.eur:
            float curr3 = Float.parseFloat(num1) * Float.parseFloat(String.valueOf(eur));
            result.setText(String.valueOf(curr3)+ " €");
            break;

        case R.id.gbp:
            float curr4 = Float.parseFloat(num1) * Float.parseFloat(String.valueOf(gbp));
            result.setText(String.valueOf(curr4) + " £");
            break;

app log:---------崩溃开始 E / AndroidRuntime:致命异常:主要                   处理:com.example.martin.currencyconverter,PID:2381                   java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.martin.currencyconverter / com.example.martin.currencyconverter.fromdkk}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.Button。 nullO对象引用上的setOnClickListener(android.view.View $ OnClickListener)'                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)

logcat:11-26 16:28:41.191 2247-2438 / com.example.martin.currencyconverter I / OpenGLRenderer:Initialized EGL,1.4版 11-26 16:28:41.191 2247-2438 / com.example.martin.currencyconverter D / OpenGLRenderer:交换行为1 11-26 16:28:41.316 2247-2438 / com.example.martin.currencyconverter E / EGL_emulation:tid 2438:eglSurfaceAttrib(1146):错误0x3009(EGL_BAD_MATCH) 11-26 16:28:41.316 2247-2438 / com.example.martin.currencyconverter W / OpenGLRenderer:无法在表面0x9f9fec20上设置EGL_SWAP_BEHAVIOR,错误= EGL_BAD_MATCH

1 个答案:

答案 0 :(得分:0)

您必须首先初始化按钮视图,例如

如果你的xml中你定义了像这样的按钮

 declare @text varchar(100)=(select [text] from Mytable where id = 1)
 declare @bit bit = (select [bit] from Mytable where id = 1)
 declare @Newtext varchar(100)
 declare @Newbit bit
 declare @Mytable table(id int, [text] varchar(100), [bit] bit) 
 Insert into  @Mytable select id,text, bit from Mytable where id = 1
 declare @counter int =2

 while @counter<=(select COUNT(*) from MyTable)
 Begin
    select @Newtext=(select [text] from Mytable where id = @counter)
    select @Newbit=(select [bit] from Mytable where id = @counter)

    IF @Newtext!=@text or @Newbit!=@bit
    Begin   
        Insert into @Mytable 
        select * from Mytable where id = @counter
    End
    set @text = @Newtext
    set @bit = @Newbit;
    set @counter = @counter+1
 END
 select * from @Mytable

在你的活动中

            <TextView
                android:id="@+id/mybtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="16dp"
                android:layout_marginTop="16dp"
                android:text="Click me!"
                />

在此行之后按钮被取消,然后只有你可以添加列表器

如果你在Button b = (Button) findViewById(R.id.mybtn); 你已经写了

*Fragment*

其中假定rootView是膨胀的片段视图。