Android代码无效

时间:2016-04-06 08:13:09

标签: android error-handling

有人可以指导我吗? 当我运行它时,它会在运行之前停止。 我不知道这有什么不对。 有人可以指导我吗? 当我运行它时,它会在运行之前停止。 我不知道这有什么不对。

MainActivity.java -

package gangster.cookies;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

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

TextView textView = (TextView) findViewById(R.id.textView);
ImageView imageView = (ImageView) findViewById(R.id.bc);

public void eat(View view) {

    textView.setText(R.string.changed_shit);
    imageView.setImageResource(R.drawable.after_cookie);

}

public void uneat(View view) {
    textView.setText(R.string.changed_again);
    imageView.setImageResource(R.drawable.before_cookie);
}
}

的strings.xml -

<resources>
<string name="app_name">Cookies</string>
<string name="changed_shit">I am full bitch</string>
<string name="changed_again">Feed me again Motherfucker</string>
</resources>

activity_main.xml中 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BC8F8F"
android:orientation="vertical"
tools:context="gangster.cookies.MainActivity">

<ImageView
    android:id="@+id/bc"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    android:scaleType="centerCrop"
    android:src="@drawable/before_cookie" />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:text="I am Hungry Bitch"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30sp"
        android:textColor="#fff"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Eat Cookie"
        android:id="@+id/button"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="30dp"
        android:onClick="eat"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Uneat"
        android:id="@+id/button2"
        android:layout_marginRight="30dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="100dp"
        android:onClick="uneat"/>
        </LinearLayout>

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

更改您的onCreate视图,如:

TextView textView;
ImageView imageView;
Button eat;


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
imageView = (ImageView) findViewById(R.id.bc);
eat = (Button) findViewById(R.id.button);

eat.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      eat();

                    }
                });



// Calling member function

  uneat();

  }

  public void eat() {

  textView.setText(R.string.changed_shit);
  imageView.setImageResource(R.drawable.after_cookie);

    }

   public void uneat() {
   textView.setText(R.string.changed_again);
   imageView.setImageResource(R.drawable.before_cookie);
   }

答案 1 :(得分:0)

您已在任何方法之外初始化 TextView ImageView ..将该代码放在 onCreate()。方法中。

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

    TextView textView = (TextView) findViewById(R.id.textView);
    ImageView imageView = (ImageView) findViewById(R.id.bc);
}

答案 2 :(得分:0)

使用以下代码替换 MainActivity.java

package gangster.cookies;

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


public class MainActivity extends Activity {

TextView textView;
ImageView imageView;

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

    textView = (TextView) findViewById(R.id.textView);
    imageView = (ImageView) findViewById(R.id.bc);
}

public void eat(View view) {

    textView.setText(R.string.changed_shit);
    imageView.setImageResource(R.drawable.after_cookie);

}

public void uneat(View view) {
    textView.setText(R.string.changed_again);
    imageView.setImageResource(R.drawable.before_cookie);
}
}