2016-12-16 71 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它将很好地工作。