2016-12-16 100 views
0

這是我MainActivity.java /* * 版權所有(C)2016年Android開源項目 * *在Apache許可2.0版(簡稱 「許可證」); *除遵守許可證外,您不得使用此文件。 *您可以在獲得許可證的副本 * * http://www.apache.org/licenses/LICENSE-2.0 * *除非適用法律要求或書面同意,根據許可證分發的軟件 *分佈在「原樣」的基礎, *沒有任何形式的保證或條件,無論是明示還是暗示。 *請參閱許可證以瞭解許可證下的特定語言管理權限和 *限制。 */ package com.example.android.miwok;我onClickListener不工作

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


public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Set the content of the activity to use the activity_main.xml layout file 
    setContentView(R.layout.activity_main); 

    TextView numbers1=(TextView)findViewById(R.id.numbers); 
    assert numbers1 != null; 
    numbers1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent number_intent =new Intent(MainActivity.this,NumbersActivity.class); 
      startActivity(number_intent); 
     } 
    }); 

    setContentView(R.layout.activity_main); 

    TextView family=(TextView)findViewById(R.id.family); 
    assert family != null; 
    family.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent family_intent=new Intent(MainActivity.this,FamilyActivity.class); 
      startActivity(family_intent); 
     } 
    }); 

    setContentView(R.layout.activity_main); 

    TextView colors=(TextView)findViewById(R.id.colors); 
    assert colors != null; 
    colors.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent color_intent=new Intent(MainActivity.this,ColorsActivity.class); 
      startActivity(color_intent); 
     } 
    }); 

    setContentView(R.layout.activity_main); 

    TextView phrases=(TextView)findViewById(R.id.phrases); 
    assert phrases != null; 
    phrases.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent phrases_intent=new Intent(MainActivity.this,PhrasesActivity.class); 
      startActivity(phrases_intent); 
      } 
     }); 
    } 


} 

和我main_activity.xml

<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2016 The Android Open Source Project 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

     http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
--> 
<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="@color/tan_background" 
android:orientation="vertical" 
tools:context="com.example.android.miwok.MainActivity"> 

<TextView 
    android:id="@+id/numbers" 
    style="@style/CategoryStyle" 
    android:background="@color/category_numbers" 
    android:text="@string/category_numbers" /> 

<TextView 
    android:id="@+id/family" 
    style="@style/CategoryStyle" 
    android:background="@color/category_family" 
    android:text="@string/category_family" /> 

<TextView 
    android:id="@+id/colors" 
    style="@style/CategoryStyle" 
    android:background="@color/category_colors" 
    android:text="@string/category_colors" /> 

<TextView 
    android:id="@+id/phrases" 
    style="@style/CategoryStyle" 
    android:background="@color/category_phrases" 
    android:text="@string/category_phrases" /> 

</LinearLayout> 

和磨片我運行這個程序沒有錯誤,沒有warings但是當我在我的文字點擊查看其不會去任何其他活動除外短語文本視圖,它正在爲短語的TextView

回答

0

爲什麼你重新設置setContentView(R.layout.activity_main);又一次

只需設置一次的下方

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Set the content of the activity to use the activity_main.xml layout file 
    setContentView(R.layout.activity_main); 
    . 
    . 
    . 
    } 
+0

謝謝你它爲我工作謝謝你非常感謝你 – Rizwan

+0

@Rizwan偉大的你可以打勾標記,如果它有幫助。 –

+0

我沒有足夠的批次或聲望 – Rizwan

0
mport android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.indianic.signaturestylo.R; 


public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.activity_main); 

     TextView numbers1=(TextView)findViewById(R.id.numbers); 
     assert numbers1 != null; 
     numbers1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent number_intent =new Intent(MainActivity.this,NumbersActivity.class); 
       startActivity(number_intent); 
      } 
     }); 


     TextView family=(TextView)findViewById(R.id.family); 
     assert family != null; 
     family.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent family_intent=new Intent(MainActivity.this,FamilyActivity.class); 
       startActivity(family_intent); 
      } 
     }); 


     TextView colors=(TextView)findViewById(R.id.colors); 
     assert colors != null; 
     colors.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent color_intent=new Intent(MainActivity.this,ColorsActivity.class); 
       startActivity(color_intent); 
      } 
     }); 


     TextView phrases=(TextView)findViewById(R.id.phrases); 
     assert phrases != null; 
     phrases.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent phrases_intent=new Intent(MainActivity.this,PhrasesActivity.class); 
       startActivity(phrases_intent); 
      } 
     }); 
    } 


} 

更改與此你的Java COE它將很好地工作。