1

所以,

我正在构建一个具有 2 个活动的基本应用程序。现在,第一个效果很好,第二个不行。在我编辑代码时它没有显示任何错误,但是当我运行应用程序并且当我处于第二个活动时,它给了我一个恼人的错误。说“意外停止......”你知道的。我尝试调试但失败了。在名为 Troll.java 的第二个活动中,我应该能够选中一个复选框,并且当我这样做时,文本应该更改为其他内容。它说停止 unexpe .....

主.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:padding="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<include
 android:layout_centerInParent="true"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 layout="@layout/resuable_pizza_layout" />

<TextView
    android:id="@+id/display"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="105dp"
    android:text="@string/display"
    android:textColor="#FFFFFF"
    android:textSize="18sp" />

<ImageView
    android:id="@+id/lol"
    android:onClick="onlol"
    android:contentDescription="@string/lol2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/display"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="66dp"
    android:src="@drawable/lol" />

巨魔.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"
android:background="#FF69B4"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <CheckBox
        android:id="@+id/box1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onChecked1"
        android:text="@string/ch1"
        android:textColor="#000000" />

    <CheckBox
        android:id="@+id/box2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onChecked2"
        android:text="@string/ch2"
        android:textColor="#000000" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:gravity="center"
    android:layout_weight="0.26" >

    <TextView
        android:id="@+id/textView1"
        android:textColor="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/trollFace"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

</LinearLayout>

主要活动.java:

package com.example.pizza2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity {

CheckBox pepp, cheese;
TextView tv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pepp = (CheckBox) findViewById(R.id.check1);
    cheese = (CheckBox) findViewById(R.id.check2);
    tv = (TextView) findViewById(R.id.display);

}

public void onShow(View view){
    StringBuilder str = new StringBuilder();
    if (pepp.isChecked() && cheese.isChecked()){
        str.append("Pepperoni, extra cheese pizza!");
    }

    else if (pepp.isChecked()){
        str.append("Pepperoni pizza!");
    }

    else if (cheese.isChecked()){
        str.append("Extra cheese pizza!");
    }

    tv.setText(str);
}

public void onlol(View view){
    Intent intent = new Intent(this, Troll.class);
    startActivity(intent);
}

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

    }

巨魔.java:

package com.example.pizza2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

 public class Troll extends Activity{

CheckBox box11, box22;
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.troll);
    box11 = (CheckBox) findViewById(R.id.box1);
    box22 = (CheckBox) findViewById(R.id.box2);
    tv = (TextView) findViewById(R.id.display);

}

public void onChecked1(View view){

    StringBuilder str = new StringBuilder();

    if(box11.isChecked()){
    str.append("Lol bro!");
    }

    tv.setText(str);
}//end of "onChecked1"  


    public void onChecked2(View view){
        StringBuilder str = new StringBuilder();
        if(box22.isChecked()){
            str.append("XD XD Kaki");
        }
        tv.setText(str);

    } //end of "onChecked2" 
 }
4

0 回答 0