Android应用在按钮点击时不断崩溃

时间:2013-11-27 19:06:16

标签: android eclipse button android-emulator crash

我查看了其他Q和答案,但发现我的代码没有任何问题? 我必须做错事,所以如果有人能帮助我,我将不胜感激。 我检查了第1页和第2页以及清单。

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>

对不起延迟log cat(不确定如何发布所有内容)

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.

2 个答案:

答案 0 :(得分:2)

没有堆栈跟踪,很难说清楚。从我所看到的,你有两个可能的罪魁祸首。

<强>第一

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"

看看是否有效。

答案 1 :(得分:0)

问题应该是:

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

        }
    });

希望它对你有所帮助。