应用程序停止在Android模拟器 [英] Application is stopped in android emulator

查看:147
本文介绍了应用程序停止在Android模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是完全新的Java,所以我已经开始制作一个应用程序来找到presing的总加减按钮。我已经定义按钮和 FRAGMENT_MAIN.XML 文本视图,以及他们在主activity.JAVA功能。定义主按钮的功能后, activity.java ,我收到logcat的错误在我的程序和应用程序在Android模拟器得到不幸采空。我使用的Andr​​oid SDK版本22.6.4,对Java develpers版本的Eclipse IDE是2.0.2.20140224-0000和Eclipse平台的版本是4.3.2.v2.140221-1852。

I'm completely new to java, so i have started making an app to find the TOTAL by presing add and subtract buttons. I have defined buttons and the text view in FRAGMENT_MAIN.XML, and their functions in main activity.JAVA . After defining the functions of buttons in main activity.java, I'm getting logcat errors in my program and the app is getting unfortunately stoped in android emulator. i am using android sdk version 22.6.4, eclipse ide for java develpers version is 2.0.2.20140224-0000 and eclipse platform version is 4.3.2.v2.140221-1852.

我MainActivity.java

my MainActivity.java

package com.counterehr.abhi;

public class MainActivity extends ActionBarActivity {

    int counter;
    Button add, sub;
    TextView display;   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter = 0;
        add = (Button) findViewById(R.id.bAdd);
        sub = (Button) findViewById(R.id.bSub);
        display = (TextView) findViewById(R.id.tvDisplay);
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                display.setText("Your total is " + counter);
            }
        });
  sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter--;
                display.setText("Your total is " + counter);
            }
        });

  if (savedInstanceState == null) {
      getSupportFragmentManager().beginTransaction()
              .add(R.id.container, new PlaceholderFragment())
              .commit();
  }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

FRAGMENT_MAIN.XM L为

FRAGMENT_MAIN.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_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.counterehr.abhi.MainActivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your total is 0" 
        android:textSize="45sp"
        android:layout_gravity="center"
        android:gravity="center" 
        android:id="@+id/tvDisplay"/>

        <Button
            android:id="@+id/bAdd"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvDisplay"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="46dp"
            android:text="Add one"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bSub"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/bAdd"
            android:layout_centerVertical="true"
            android:text="Subtract one"
            android:textSize="20sp" />

</RelativeLayout>

上运行我得到的logcat错误,因为该应用程序。

on running the application i am getting logcat errors as.

logcat的错误

    07-01 18:16:17.963: E/Trace(833): error opening trace file: No such file or directory (2)
    07-01 18:16:19.675: D/dalvikvm(833): GC_FOR_ALLOC freed 80K, 9% free 2419K/2644K, paused 356ms, total 356ms
    07-01 18:16:19.704: I/dalvikvm-heap(833): Grow heap (frag case) to 3.415MB for 960016-byte allocation
    07-01 18:16:19.802: D/dalvikvm(833): GC_FOR_ALLOC freed 1K, 7% free 3355K/3584K, paused 97ms, total 97ms
    07-01 18:16:20.002: D/dalvikvm(833): GC_CONCURRENT freed <1K, 7% free 3355K/3584K, paused 9ms+33ms, total 201ms
    07-01 18:16:20.382: D/dalvikvm(833): GC_CONCURRENT freed <1K, 6% free 3779K/4004K, paused 5ms+4ms, total 52ms
    07-01 18:16:20.543: D/AndroidRuntime(833): Shutting down VM
    07-01 18:16:20.543: W/dalvikvm(833): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    07-01 18:16:20.583: E/AndroidRuntime(833): FATAL EXCEPTION: main
    07-01 18:16:20.583: E/AndroidRuntime(833): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.counterehr.abhi/com.counterehr.abhi.MainActivity}: java.lang.NullPointerException
    07-01 18:16:20.583: E/AndroidRuntime(833):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at android.os.Handler.dispatchMessage(Handler.java:99)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at android.os.Looper.loop(Looper.java:137)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at android.app.ActivityThread.main(ActivityThread.java:5041)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at java.lang.reflect.Method.invokeNative(Native Method)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at java.lang.reflect.Method.invoke(Method.java:511)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at dalvik.system.NativeStart.main(Native Method)
    07-01 18:16:20.583: E/AndroidRuntime(833): Caused by: java.lang.NullPointerException
    07-01 18:16:20.583: E/AndroidRuntime(833):  at com.counterehr.abhi.MainActivity.onCreate(MainActivity.java:33)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at android.app.Activity.performCreate(Activity.java:5104)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    07-01 18:16:20.583: E/AndroidRuntime(833):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    07-01 18:16:20.583: E/AndroidRuntime(833):  ... 11 more
    07-01 18:16:23.873: I/Process(833): Sending signal. PID: 833 SIG: 9

请!有人帮我出这一点,我想一切可能的事情,但没有得到解决。

please! someone help me out of this, i tried every possible thing but not getting the solution.

推荐答案

的问题是,你是混合了片段和活动,正因为如此,处理事件的活动,尽管这些意见都显示在片段。基本上你是为了在该活动的FrameLayout,这就是它 - 逻辑在片段处理

The problem is that you are mixing up Fragments and Activities, and as such, handling events in the Activity, even though these Views are displayed in the Fragment. Basically you are meant to have a FrameLayout in the Activity, and that's it - the logic is handled in the Fragment.

的一个例子是以下

public class ContainerActivity extends ActionBarActivity
{
    @Override
    public void onCreate(Bundle saveInstanceState)
    {
        super.onCreate(saveInstanceState);
        this.setContentView(R.layout.activity_container);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (saveInstanceState == null)
        {               
             getSupportFragmentManager().beginTransaction()
                .add(R.id.activity_container_container, new ExampleFragment())
                .addToBackStack(null)
             .commit();
        }
        getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener()
        {
            public void onBackStackChanged()
            {
                int backCount = getSupportFragmentManager().getBackStackEntryCount();
                if (backCount == 0)
                {
                    finish();
                }
            }
        });
    }
}

activity_container.xml:

activity_container.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/activity_container_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

ExampleFragment:

ExampleFragment:

public class ExampleFragment extends Fragment implements View.OnClickListener
{
    private Button btnOne;
    private Button btnTwo;
    private Button btnThree;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_example, container, false);

        btnOne = (Button) rootView.findViewById(R.id.example_button_one);
        btnTwo = (Button) rootView.findViewById(R.id.example_button_two);
        btnThree = (Button) rootView.findViewById(R.id.example_button_three);

        btnOne.setOnClickListener(this);
        btnTwo.setOnClickListener(this);
        btnThree.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v)
    {
        if (btnOne == v)
        {
            Toast.makeText(getActivity(), "One.", Toast.LENGTH_LONG).show();
        }
        else if (btnTwo == v)
        {
            Toast.makeText(getActivity(), "Two.", Toast.LENGTH_LONG).show();
        }
        else if (btnThree == v)
        {
            Toast.makeText(getActivity(), "Three.", Toast.LENGTH_LONG).show();
        }
    }
}

fragment_example.xml:

fragment_example.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/example_button_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30dp"
            android:text="@string/hello" 
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>

        <Button
            android:id="@+id/example_button_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/example_button_one"
            android:layout_alignRight="@+id/example_button_one"
            android:layout_below="@+id/example_button_one"
            android:layout_marginTop="30dp"
            android:text="@string/hello" />

        <Button
            android:id="@+id/example_button_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/example_button_two"
            android:layout_alignRight="@+id/example_button_two"
            android:layout_below="@+id/example_button_two"
            android:layout_marginTop="30dp"
            android:text="@string/hello" />

</RelativeLayout>

Android系统的Manifest.xml:

Android-Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        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.ContainerActivity"
            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>

这应该是一个有效的例子,它显示了如何使用一个活动来显示片段,并处理该分段的事件。目前,它不显示片段如何与活动通信切换到另一个片段,它是由一个界面,你会做一个变量并存储在onAttach()回调方法做的方式。

And that should be a valid example, it shows how you can use an Activity to display a Fragment, and handle events in that Fragment. It currently doesn't show how the Fragment communicates with the Activity to change to another Fragment, which is by the way done by an interface, which you would make a variable to and store in the onAttach() callback method.

但我没有包括以及在<一场以轻度精版href=\"http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate\">NullPointerException在访问的onCreate意见()

这篇关于应用程序停止在Android模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆