为什么我的Android应用程序没有运行(不幸的是应用程序停止了)

时间:2014-06-10 16:16:24

标签: android eclipse manifest imagebutton

我知道有几个与此类似的问题,但我尝试了不同的建议,例如清洁和重建,但仍然没有任何对我有用。我是Android新手,也是Eclipse的新手。我正在尝试创建一个简单的测试应用程序,用户单击图像按钮(一个球),并且有一个textview,当单击该按钮时会更改文本。

由于我是android的新手,我注意到有一个片段main.xml和一个main.xml,所以我想知道是否有2个。

有谁知道为什么这不能在模拟器或设备上运行?谢谢你的帮助。

主要 -        

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

  </menu>

片段主要 -

   <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.example.imagebutton.MainActivity$PlaceholderFragment" >

   <ImageButton
    android:id="@+id/ball"
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:scaleType="fitXY"
    android:src="@drawable/ball"
     />

   <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/ball"
    android:layout_marginBottom="36dp"
    android:layout_marginLeft="48dp"
    android:layout_toRightOf="@+id/ball"
    android:text="@string/click" />

   </RelativeLayout>

主要活动 -         package com.example.imagebutton;

   import android.app.Activity;
   import android.app.ActionBar;
   import android.app.Fragment;
   import android.os.Bundle;
   import android.view.LayoutInflater;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.Button;
   import android.widget.ImageButton;
   import android.widget.TextView;
   import android.os.Build;

 public class MainActivity extends Activity {

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

    ImageButton ballButton = (ImageButton) findViewById(R.id.ball);

    ballButton.setOnClickListener(new View.OnClickListener(){

        @Override

        public void onClick (View v){

        TextView text = (TextView) findViewById(R.id.tv);

        text.setText("It has been clicked!");
        }



        });
     }
 }

清单 -               

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.imagebutton.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

  </manifest>

1 个答案:

答案 0 :(得分:0)

这是因为findViewById()activity_main布局中搜索,而按钮位于片段的布局fragment_main中。

在片段的onCreateView()方法中移动该段代码:

//...
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageButton ballButton = (ImageButton) findViewById(R.id.ball);
ballButton.setOnClickListener(new View.OnClickListener(){    
        @Override    
        public void onClick (View v){    
        TextView text = (TextView) findViewById(R.id.tv);    
        text.setText("It has been clicked!");
        }            
 });
//...

请注意,现在您可以通过rootView view访问它:

ImageButton ballButton = (ImageButton)rootView.findViewById(R.id.ball);

否则你会再次NullPointerException

相关问题