Android 应用程序已停止工作 - Eclipse

Posted

技术标签:

【中文标题】Android 应用程序已停止工作 - Eclipse【英文标题】:Android application has stopped working- Eclipse 【发布时间】:2014-12-08 13:25:03 【问题描述】:

我是 android 开发的初学者。我在模拟器和我的真实 Android 设备中都收到此错误消息。请帮帮我!!

public class MainActivity extends ActionBarActivity 
     Button btn1;
     TextView tv1;
     EditText edt1;
     String msj= "d";       

double n = Double.valueOf(edt1 . getText().toString());
double a = (1 / (Math.sqrt(5))); 
double b = ((1+(Math.sqrt(5)))/2); 
double c = ((1-(Math.sqrt(5)))/2); 
double i = a * ((Math.pow(b,n)) - (Math.pow(c,n)));
String k = String.valueOf(i);

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

   setContentView(R.layout.activity_main);
    btn1 = (Button) findViewById(R.id.btn1);
    final TextView tv1 = (TextView) findViewById(R.id.tv1);
    final EditText edt1 = (EditText) findViewById(R.id.edt1);

    btn1.setOnClickListener(new OnClickListener()          
        @Override
        public void onClick(View v)                
            msj = edt1.getText().toString();
            tv1.setText( k );               
        
    );



@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);

XML 代码;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
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.fibonacci.MainActivity" >

<EditText
    android:id="@+id/edt1"
    android:layout_
    android:layout_
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:hint="@string/girdi"
    android:inputType="number" >

</EditText>

<Button
    android:id="@+id/btn1"
    android:layout_
    android:layout_
    android:layout_below="@+id/edt1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:text="@string/buton" />

<TextView
    android:id="@+id/tv1"
    android:layout_
    android:layout_
    android:layout_alignLeft="@+id/edt1"
    android:layout_below="@+id/btn1"
    android:layout_marginTop="37dp"
    android:text="@string/cikti" />

</RelativeLayout>

我无法发布日志猫。因为字数限制。 这是 LogCat:

    0-13 17:10:48.907: I/Choreographer(380): Skipped 30 frames!  The application may be doing too much work on its main thread.
10-13 17:10:48.927: E/AndroidRuntime(1021): FATAL EXCEPTION: main
10-13 17:10:48.927: E/AndroidRuntime(1021): Process: com.example.fibonacci, PID: 1021
10-13 17:10:48.927: E/AndroidRuntime(1021): java.lang.RuntimeException: Unable to instantiate activity ComponentInfocom.example.fibonacci/com.example.fibonacci.MainActivity: java.lang.NullPointerException
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.os.Looper.loop(Looper.java:136)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at java.lang.reflect.Method.invoke(Method.java:515)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at dalvik.system.NativeStart.main(Native Method)
10-13 17:10:48.927: E/AndroidRuntime(1021): Caused by: java.lang.NullPointerException
10-13 17:10:48.927: E/AndroidRuntime(1021):     at com.example.fibonacci.MainActivity.<init>(MainActivity.java:20)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at java.lang.Class.newInstanceImpl(Native Method)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at java.lang.Class.newInstance(Class.java:1208)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
10-13 17:10:48.927: E/AndroidRuntime(1021):     ... 11 more
10-13 17:10:48.977: I/Choreographer(380): Skipped 47 frames!  The application may be doing too much work on its main thread.
10-13 17:10:48.977: W/ActivityManager(380):   Force finishing activity com.example.fibonacci/.MainActivity
10-13 17:10:49.387: I/WindowManager(380): Screenshot max retries 4 of Tokenb2f8e630 ActivityRecordb2f8e4d0 u0 com.example.fibonacci/.MainActivity t3 f appWin=Windowb30e4a38 u0 Starting com.example.fibonacci drawState=4
10-13 17:10:49.387: W/WindowManager(380): Screenshot failure taking screenshot for (320x480) to layer 21010

感谢您的帮助。

【问题讨论】:

【参考方案1】:

正如@Sunil 所指出的那样,这可能会让你更接近你的代码来工作。您必须在 onClick 方法中进行计算。

public class MainActivity extends ActionBarActivity 
    Button btn1;
    TextView tv1;
    EditText edt1;
    String msj= "d";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);
        tv1 = (TextView) findViewById(R.id.tv1);
        edt1 = (EditText) findViewById(R.id.edt1);
        btn1.setOnClickListener(new OnClickListener() 

            @Override
            public void onClick(View v) 
                double n = Double.valueOf(edt1.getText().toString());
                double a = (1 / (Math.sqrt(5))); 
                double b = ((1+(Math.sqrt(5)))/2); 
                double c = ((1-(Math.sqrt(5)))/2); 
                double i = a * ((Math.pow(b,n)) - (Math.pow(c,n)));
                String k = String.valueOf(i);
                msj = edt1.getText().toString();
                tv1.setText( k );               
            
        );

    


【讨论】:

【参考方案2】:

你在这一行有一个错误:

double n = Double.valueOf(edt1 . getText().toString());

在你的类中,n 是一个类级别的变量,它在类加载时被初始化。它在 edt1 初始化之前被初始化。此时 edt1 为空,这基本上会引发错误。您提供的链接中有太多日志信息,我不确定是否还有其他错误,但这需要确定。

【讨论】:

【参考方案3】:

我改了代码,结果还是一样:(

package com.example.fibonacci;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity 
    Button btn1;
    TextView tv1;
    EditText edt1;
    String msj= "d";

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

        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);
        tv1 = (TextView) findViewById(R.id.tv1);
        edt1 = (EditText) findViewById(R.id.edt1);

        btn1.setOnClickListener(new OnClickListener() 

            @Override
            public void onClick(View v) 

                double n = Double.valueOf(edt1.getText().toString());
                double a = (1 / (Math.sqrt(5))); 
                double b = ((1+(Math.sqrt(5)))/2); 
                double c = ((1-(Math.sqrt(5)))/2); 
                double i = a * ((Math.pow(b,n)) - (Math.pow(c,n)));
                String k = String.valueOf(i);
                msj = edt1.getText().toString();
                tv1.setText( k );                           
            
        );
    

    @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);
    

【讨论】:

您将我的答案标记为正确,那么您是否设法解决了这个问题?如果没有,请附上您的 logcat。此外,您在上面的问题中附加的 logcat 太大,因为它是整个系统 logcat。您不希望这样 - 您只想查看您的应用创建的日志,因此我建议您过滤您的 logcat 日志以仅获取来自您的应用的日志。 ***.com/questions/6854127/…

以上是关于Android 应用程序已停止工作 - Eclipse的主要内容,如果未能解决你的问题,请参考以下文章

android studio 虚拟机adb.exe已停止工作的处理

Android 定位服务已停止

不幸的是,Apps已停止在某些Android设备上

websocketproxy已停止工作

这个 Java 类可以顺利编译,但是当我使用我的 android 手机作为 ADB 时单击它。它说设备已停止工作

android studio adb.exe已停止工作