【问题标题】:Android app keeps crashing on button clickAndroid应用程序在按钮单击时不断崩溃
【发布时间】:2013-11-27 19:06:15
【问题描述】:

我查看了其他问题和答案,但找不到我的代码有什么问题? 我一定做错了什么,所以如果有人可以帮助我,将不胜感激。 我检查了第一页和第二页以及清单。

package com.example.fmf;

import com.example.fmf.Search;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.view.View.OnClickListener;


public class MainActivity extends Activity {






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

        Button button = (Button) findViewById(R.id.go_button);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                   goMenu();
            }

        });
    }


    public void goMenu()
    {
      Intent myIntent = new Intent(this, Search.class);
      startActivity(myIntent);
    } 


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

}

第 2 页

package com.example.fmf;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Spinner;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class Search extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    // Show the Up button in the action bar.
    setupActionBar();
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.search, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-       vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Bundle extras = getIntent().getExtras();
 public final String ID_CITY = extras.getString("CITY_TH");

//retrieve list view value selected and store in variable restaurant
//Not sure how to do this...



}

清单

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
    <activity
        android:name=".Search"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
    <activity
        android:name=".Info"
        android:label="@string/app_name"
        android:parentActivityName=".Search" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".Search" />
        </activity>
    </application>

</manifest>

对不起,日志猫耽误了(不知道如何发布所有内容)

11-27 15:40:05.118: D/AndroidRuntime(771): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-27 15:40:05.128: D/AndroidRuntime(771): CheckJNI is ON
11-27 15:40:05.158: D/dalvikvm(771): Trying to load lib libjavacore.so 0x0
11-27 15:40:05.168: D/dalvikvm(771): Added shared lib libjavacore.so 0x0
11-27 15:40:05.198: D/dalvikvm(771): Trying to load lib libnativehelper.so 0x0
11-27 15:40:05.198: D/dalvikvm(771): Added shared lib libnativehelper.so 0x0
11-27 15:40:05.278: E/cutils-trace(771): Error opening trace file: No such file or directory (2)
11-27 15:40:05.868: D/AlertService(549): Beginning updateAlertNotification
11-27 15:40:05.898: D/AndroidRuntime(771): Calling main entry com.android.commands.am.Am
11-27 15:40:05.918: D/dalvikvm(771): Note: class Landroid/app/ActivityManagerNative; has 163 unimplemented (abstract) methods
11-27 15:40:05.948: I/ActivityManager(286): Force stopping package com.example.fmf appid=10048 user=-1
11-27 15:40:05.959: I/ActivityManager(286): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.fmf/.MainActivity} from pid 771
11-27 15:40:06.328: I/WindowManager(286): Screenshot max retries 4 of Token{419849f0 ActivityRecord{41984880 u0 com.android.launcher/com.android.launcher2.Launcher}} appWin=Window{419646b0 u0 com.android.launcher/com.android.launcher2.Launcher} drawState=4
11-27 15:40:06.328: W/WindowManager(286): Screenshot failure taking screenshot for (480x800) to layer 21000
11-27 15:40:06.338: D/AndroidRuntime(771): Shutting down VM
11-27 15:40:06.348: D/dalvikvm(771): GC_CONCURRENT freed 92K, 16% free 513K/608K, paused 1ms+0ms, total 11ms
11-27 15:40:06.358: D/AlertService(549): No fired or scheduled alerts
11-27 15:40:06.388: W/dalvikvm(783): PR_CAPBSET_DROP 0 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-27 15:40:06.388: W/dalvikvm(783): PR_CAPBSET_DROP 1 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.

【问题讨论】:

  • 请发布堆栈跟踪
  • 已查找跟踪文件错误,但发现它通常是由 setContentView(R.layout.activity_main); 放置在错误的位置引起的,但我很确定它对我来说是在正确的位置?
  • 请从 logcat 发布您的应用程序的异常堆栈跟踪,而不仅仅是一些随机的 logcat 行。

标签: android eclipse button android-emulator crash


【解决方案1】:

如果没有堆栈跟踪,可能很难分辨。据我所知,你有两个可能的罪魁祸首。

第一

Bundle extras = getIntent().getExtras();
public final String ID_CITY = extras.getString("CITY_TH");

在您的 Search 类中,您尝试从 bundle 中获取数据。但是在您的 MainActivity.goMenu() 中,您没有传递捆绑包。这可能会导致NULLPOINTEREXCEPTION

第二

setupActionBar();

请尝试在您的 Search.class 中将其注释掉,看看结果如何。我从清单中看不到您的主题是什么。因此,您甚至可能没有可能导致崩溃的操作栏。因此,如果注释掉上面的行有效,请将以下内容添加到您的清单中:

变化:

android:theme="@style/AppTheme"

收件人:

android:theme="@android:style/Theme.Holo"

看看是否可行。

【讨论】:

  • 打败我。绝对值得将这两行注释掉,看看问题是否消失。
  • 另外,一旦它们开始工作,它们应该被移到像onCreate()这样的方法中
  • 我一直在尝试将清单更改为android:theme="@style/Theme.Holo",但我一直收到错误消息,提示找不到资源? @Brianjs
  • 没有,当我按下模拟器上的按钮时,应用程序仍然崩溃。将setupActionBar(); 注释掉并删除Bundle extras = getIntent().getExtras(); public final String ID_CITY = extras.getString("CITY_TH");? @Brianjs 感谢您的帮助
  • 你注释掉 setupActionBar() 和两条 Bundle 行了吗?
【解决方案2】:

问题应该是这样的:

button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
               goMenu();
        }

    });
}


public void goMenu()
{
  Intent myIntent = new Intent(this, Search.class);
  startActivity(myIntent);
} 

尝试替换这个:

button.setOnClickListener(new View.OnClickListener() {
       @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(MainActivity.this,Search.class);
            MainActivity.this.startActivity(myIntent);

        }
    });

希望对你有所帮助。

【讨论】:

  • 谢谢,但我最初有你的建议并将其更改为当前的以尝试解决问题,所以我不认为就是这样(尽管你的可能更好)。 @user2675569
猜你喜欢
  • 2017-07-04
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多