开发者

Application not working, beginner

i am fairly new to android programming and usually find my answers to my problems by searching, but this one i just cant and its very confusing. The code itself doesn't show any signs of problems, well i do get 2 java exception breakpoints but i dont know how to fix those as they are "unknown"but when i run it on the emulator it says the application has stopped unexpectedly force close. I try to debug it but i dont know how to do it that well. any way here are the codes btw the app is just a test all it to do is have buttons that take me to other "pages" and back. I would appreciate any help.

Main java file

package com.simbestia.original;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class original extends Activity implements View.OnClickListener {
    Button button1, button2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button1 = (Button) findViewById(R.id.pagetwo);
        button2 = (Button) findViewById(R.id.main);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.pagetwo:
            setContentView(R.layout.pagetwo);
            break;
        case R.id.main:
            setContentView(R.layout.main);
            break;
        }
    }
}

Main xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
<Button android:text="pagetwo" android:id="@+id/pagetwo"              android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

Well here is what i change the code to this one is just one button but it works with multiple and i made a class for every page...

package com.simbestia.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class main extends Activity {
/** Called when 开发者_StackOverflowthe activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button mainmenu = (Button) findViewById(R.id.mainmenu);
    mainmenu.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(),  mainmenu.class);
            startActivityForResult(myIntent, 0);
        }
    });
}

}

Works how i wanted to so its all good i guess ty again


In the command line/terminal, use ./adb logcat to see, in real time, warnings, erros and such from your device, while you run your app. That should help you a lot.

Note: Don't forget to be in the right folder... <android-sdk-version>/platform-tools, that's where the ADB is.


You should learn to debug your own application. Starting with a break point right in the first line of your onCreate() method.

You can also take a look here: http://www.droidnova.com/debugging-in-android-using-eclipse,541.html

Another possibility is to add a log call in the first line of your onCreate() so you can see where the log of your app starts...

edit:

The way you want to switch the layout is wrong. Try layout switcher or start a new activity for your new layout. calling setContentView more than once is basically just wrong...


Some things to check: make sure your code source folder is the same as your package name (com.simbestia.original) ; make sure it builds (without errors) before you try and run it and make sure that your manifest file has its package attribute set to your package name (com.simbestia.original).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜