2017-11-23 4 views
0

コーディングが始まったばかりです。ImageViewをクリックするとアプリケーションがクラッシュする

HEREの説明に従いますが、ボタンonClickをImageView onClickに変更しました(画像をクリックして別のアクティビティに移動しましたが動作しません)。私は何かが欠けているのだろうかと思っています。誰も私を助けることができれば、それは素晴らしいだろう:)

import android.content.Intent; 
 
import android.os.Bundle; 
 
import android.support.v7.app.AppCompatActivity; 
 
import android.support.v7.widget.Toolbar; 
 
import android.view.View; 
 
import android.view.Menu; 
 
import android.view.MenuItem; 
 
import android.widget.ImageView; 
 

 
public class MainActivity extends AppCompatActivity { 
 

 
    ImageView hotel; **//the image that gets clicked. Don't know if i need 
 
//this in the code or not. nothing seems to point to it 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
 
     setSupportActionBar(toolbar); 
 
     getSupportActionBar().setTitle(null); 
 
    } 
 

 
    public void goToHotelWebView (View view) { 
 
     Intent intent = new Intent(this, hotelwebview.class); 
 
     startActivity(intent); **//The code that I added in from the tutorial** 
 

 
    } 
 
    @Override 
 
    public boolean onCreateOptionsMenu(Menu menu) { 
 
     // Inflate the menu; this adds items to the action bar if it is present. 
 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
 
     return true; 
 
    } 
 

 
    @Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 
     // Handle action bar item clicks here. The action bar will 
 
     // automatically handle clicks on the Home/Up button, so long 
 
     // as you specify a parent activity in AndroidManifest.xml. 
 
     int id = item.getItemId(); 
 

 
     //noinspection SimplifiableIfStatement 
 
     if (id == R.id.action_settings) { 
 
      return true; 
 
     } 
 

 
     return super.onOptionsItemSelected(item); 
 

 

 
     } 
 
    }

そして、私の.xmlページ上

は、画像はonClickのはgoToHotelWebViewに設定されています。

ありがとうございました!

EDIT = LOGCATでエラーが発生しました...これを読むと、アクションバーのデザインに問題があるように見えますが、空白のスレートで新しいアクティビティが作成され、アプリがクラッシュします。

FATAL EXCEPTION: main 
 
                      Process: com.example.wes.tripedia2, PID: 24180 
 
                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wes.tripedia2/com.example.wes.tripedia2.hotelwebview}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. 
 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) 
 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
 
                       at android.app.ActivityThread.-wrap11(Unknown Source:0) 
 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
 
                       at android.os.Handler.dispatchMessage(Handler.java:106) 
 
                       at android.os.Looper.loop(Looper.java:164) 
 
                       at android.app.ActivityThread.main(ActivityThread.java:6494) 
 
                       at java.lang.reflect.Method.invoke(Native Method) 
 
                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
 
                      Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. 
 
                       at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:201) 
 
                       at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129) 
 
                       at com.example.wes.tripedia2.hotelwebview.onCreate(hotelwebview.java:18) 
 
                       at android.app.Activity.performCreate(Activity.java:7000) 
 
                       at android.app.Activity.performCreate(Activity.java:6991) 
 
                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)  
 
                       at android.app.ActivityThread.-wrap11(Unknown Source:0)  
 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)  
 
                       at android.os.Handler.dispatchMessage(Handler.java:106)  
 
                       at android.os.Looper.loop(Looper.java:164)  
 
                       at android.app.ActivityThread.main(ActivityThread.java:6494)  
 
                       at java.lang.reflect.Method.invoke(Native Method)  
 
                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  
 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

+0

あなたのAndroidManifest.xmlで 'hotelwebview' を宣言したのですか? – diegoveloper

+0

うん。新しいアクティビティを作成するには、アプリケーションフォルダを右クリックし、「新しいアクティビティを追加」を選択して、AndroidManifest.xmlのhotelwebviewを自動的に宣言してください。 – wesdaco

+0

logcatに表示されるエラーは何ですか? – diegoveloper

答えて

0

ERROR説明This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

あなたAndroidManifestファイルをチェックして、あなたの活動 "hotelwebview" のテーマを確認してください。

<activity 
      android:name=".hotelwebview" 
      android:configChanges="orientation|keyboardHidden|screenSize" 
      android:theme="@style/AppTheme"/> 

このようなあなたのテーマを変更します。

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 
+1

ありがとうございました!あなたは大きな助けをしてきました。 – wesdaco

+0

私はエラーを調べるためにlogcatを調べることを学びました。問題は私のツールバーにあると思われます – wesdaco

+0

問題は解決しても問題は解決しない場合は、 – diegoveloper

関連する問題