2017-10-10 2 views
-1

主な活動上の2つのオブジェクトのメソッド驚くべき事実の単一のonclick

package com.example.user.amazingfacts; 

import android.graphics.Color; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 
private TextView showtext; 
private Button showbutton; 
private RelativeLayout relativeLayout; 
private Amazingnote Amazingnote=new Amazingnote(); 
private Colorwheel colorwheel=new Colorwheel(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     showtext=(TextView)findViewById(R.id.showtext); 
     showbutton=(Button)findViewById(R.id.showbutton); 
     showbutton.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
String fact= Amazingnote.getFact(); 
showtext.setText(fact); 

int color=colorwheel.getColor(); 

    relativeLayout.setBackgroundColor(color); 
showbutton.setTextColor(color); 


      } 
     }); 
    } 
} 

驚きの事実、驚くべきメモの第二の活性

package com.example.user.amazingfacts; 

import java.util.Random; 

/** 
* Created by User on 09-10-2017. 
*/ 

public class Amazingnote { 
    public String getFact() { 
     String[] facts = {"In 1889, the Queen of Italy, Margherita Savoy, ordered the first pizza delivery", 
       " You can buy eel flavored ice cream in Japan", 
       "Although the bobcat is rarely seen, it is the most common wildcat in North America", 
       " A cat's tail contains nearly 10 percent of all the bones in its body", 
       "The term astronaut comes from Greek words that mean star and sailor", 
       "The calcium in our bones and the iron in our blood come from ancient explosions of giant stars", 
       "The Nile crocodile can hold its breath underwater for up to 2 hours while waiting for prey", 
       "Jellyfish, or jellies as scientists call them, are not fish. They have no brain, no heart, and no bones", 
       " Some people used to believe that kissing a donkey could relieve a toothache.", 
       " Because the speed of Earth's rotation changes over time, a day in the age of dinosaurs was just 23 hours long", 
       "Hummingbirds' wings can beat 200 times a second.", 
       "There are more than 1,200 water parks in North America.", 
       "A seahorse can move its eyes in opposite directions—all the better to scan the water for food and predators.", 
       " To cook an egg, a sidewalk needs to be 158°F." 

     }; 

     Random randomGenerator=new Random(); 
     int randomnumber=randomGenerator.nextInt(facts.length); 
     return facts[randomnumber]; 

    } 
} 

を呼び出すTHIRD ACTIVITY -COLORWHEEL

package com.example.user.amazingfacts; 

import android.graphics.Color; 

import java.util.Random; 

/** 
* Created by User on 10-10-2017. 
*/ 

public class Colorwheel 
{ 
    //Fields or Member Variables -Properties abou the object 
    private String[] colors = { 
      "#39add1",//light blue 
      "#3079ab",//dark blue 
      "#c25975",//mauve 
      "#e15258",//red 


    }; 
    //Methods-Actions the object can take 
    int getColor() 
    { 

    Random randomGenerator=new Random(); 
    int randomnumber=randomGenerator.nextInt(colors.length); 
    int color= Color.parseColor(colors[randomnumber]) ; 
    return color ; 

} 
} 

これはAmazing facts.Itという名前の小さなアプリケーションで、2つのText viewsと1つのボタンしか使用していません。 最初のText viewはそれほど重要なものではありません "ご存知ですか?"これは最初のText viewです。 ユーザーがボタンをクリックすると、2番目のText Viewの内容とアプリケーションの背景色が変更されます。これは私の考えです。 私は正常にビルド機能を使用してアプリケーションをビルドしましたが、私はアンドロイドphone.Theアプリに私はボタンをクリックすると停止しているアプリケーションをインストールしようとしましたon clickプロパティにいくつかの問題があることを理解しています。クリックホイールの*から「カラーホイール」を選択してください。*アプリが動作しています。なぜ2つのオブジェクトのメソッドが1つのアプリケーションに読み込まれませんでしたかOn click?なぜ私はテキストビューと背景色を同時に変更できないのですか?

+1

あなたの 'relativeLayout'は初期化されていません – 0xDEADC0DE

答えて

0

変更

public class MainActivity extends AppCompatActivity { 
private TextView showtext; 
private Button showbutton; 
private RelativeLayout relativeLayout; 
private Amazingnote Amazingnote=new Amazingnote(); 
private Colorwheel colorwheel=new Colorwheel(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    relativeLayout = (RelativeLayout) findViewById(R.layout.main_activity); 
     . 
     . 
     . 

あなたのレイアウトに自分のメインの活動の最初の部分は、あなたがまだ存在しないオブジェクトの色を変更しようとしているので、初期化されていません。

+0

あなたのアクティビティはまだコンテンツビューを設定していないので、その時点でそれを行うことはできません – 0xDEADC0DE

+0

あなたは正しいです、良いキャッチ@ 0xDEADC0DE – Lavevel

+0

このアップデートはうまくいくはずです。ありがとう@ 0xDEADC0DE。それを完全に逃した。 – Lavevel

関連する問題