English 中文(简体)
开幕式上的申请坠毁
原标题:Application crashes upon opening

当我对我的安康申请进行新的更新时,申请在试图启动“选择”时便一开始(我知道什么)。 该法典:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
         Toast.makeText(this, "GPS n är aktiverad på din mobilenhet", Toast.LENGTH_SHORT).show();
    }else{
        showGPSDisabledAlertToUser();
    }

    setContentView(R.layout.about);
    Button b = (Button) findViewById(R.id.menuItem1);
    b.setOnClickListener(new View.OnClickListener() {
       public void onClick(View arg0) {
       Intent i = new Intent(nowActivity.this, About.class);
       startActivity(i);
       } 
    });
}

以及LogCat:

04-04 00:50:27.324: D/LocationManager(870): Constructor: service = android.location.ILocationManager$Stub$Proxy@44e95640
04-04 00:50:27.374: D/AndroidRuntime(870): Shutting down VM
04-04 00:50:27.374: W/dalvikvm(870): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-04 00:50:27.374: E/AndroidRuntime(870): Uncaught handler: thread main exiting due to uncaught exception
04-04 00:50:27.384: E/AndroidRuntime(870): java.lang.RuntimeException: Unable to start activity ComponentInfo{weather.right.now/weather.right.nowActivity}: java.lang.NullPointerException
04-04 00:50:27.384: E/AndroidRuntime(870):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-04 00:50:27.384: E/AndroidRuntime(870):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-04 00:50:27.384: E/AndroidRuntime(870):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-04 00:50:27.384: E/AndroidRuntime(870):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-04 00:50:27.384: E/AndroidRuntime(870):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 00:50:27.384: E/AndroidRuntime(870):  at android.os.Looper.loop(Looper.java:123)
04-04 00:50:27.384: E/AndroidRuntime(870):  at android.app.ActivityThread.main(ActivityThread.java:4363)
04-04 00:50:27.384: E/AndroidRuntime(870):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 00:50:27.384: E/AndroidRuntime(870):  at java.lang.reflect.Method.invoke(Method.java:521)
04-04 00:50:27.384: E/AndroidRuntime(870):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-04 00:50:27.384: E/AndroidRuntime(870):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-04 00:50:27.384: E/AndroidRuntime(870):  at dalvik.system.NativeStart.main(Native Method)
04-04 00:50:27.384: E/AndroidRuntime(870): Caused by: java.lang.NullPointerException
04-04 00:50:27.384: E/AndroidRuntime(870):  at weather.right.nowActivity.onCreate(nowActivity.java:37)
04-04 00:50:27.384: E/AndroidRuntime(870):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 00:50:27.384: E/AndroidRuntime(870):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-04 00:50:27.384: E/AndroidRuntime(870):  ... 11 more
04-04 00:50:27.404: I/dalvikvm(870): threadid=7: reacting to signal 3
04-04 00:50:27.424: E/dalvikvm(870): Unable to open stack trace file  /data/anr/traces.txt : Permission denied

我目前学会建立一个适当的安乐施会,因此我不知道这里有什么错误。 你们是否知道什么错误?

提前感谢!

EDIT: about.java

package weather.right;

import weather.right.now.R;
import android.app.Activity;
import android.os.Bundle;

public class About extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
    }

}

EDIT: 如今已投入使用。

package weather.right;

// import java.util.Calendar;

import weather.right.now.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
// import android.view.Menu;
// import android.view.MenuInflater;
// import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class nowActivity extends Activity {

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

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
             Toast.makeText(this, "GPS n är aktiverad på din mobilenhet", Toast.LENGTH_SHORT).show();
        }else{
            showGPSDisabledAlertToUser();
        }

        setContentView(R.layout.about);
        Button b = (Button) findViewById(R.id.menuItem1);
        b.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {
           Intent i = new Intent(nowActivity.this, About.class);
           startActivity(i);
           } 
        });
    }

    @Override
    public void onBackPressed() {
        System.exit(0);
        return;
    }
 /*
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Calendar c = Calendar.getInstance();
        // int year = c.get(Calendar.YEAR);

        // Handle item selection
        switch (item.getItemId())
        {
        case R.id.menuItem1:
            // Toast.makeText(nowActivity.this, "Copyright " + year + " Erik Edgren", 3000).show();
            setContentView(R.layout.about);
            return true;
        case R.id.menuItem2:
            System.exit(0);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    */



    public void goToSo(View view) {
        goToUrl("http://erik-edgren.nu/weather");
        System.exit(0);
    }

    private void goToUrl(String url) {
        Uri uriUrl = Uri.parse(url);
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
        startActivity(launchBrowser);
        System.exit(0);
    }



    private void showGPSDisabledAlertToUser(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setMessage("GPS-mottagaren är inte aktiverad på din mobil. För att tjänsten ska kunna hitta dig, måste den vara aktiverad.")
         .setCancelable(false)
         .setPositiveButton("Gå till inställningarna",
              new DialogInterface.OnClickListener(){
              public void onClick(DialogInterface dialog, int id){
                      Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                      startActivity(callGPSSettingIntent);
                      System.exit(0);
              }
         });
         alertDialogBuilder.setNegativeButton("Stäng",
              new DialogInterface.OnClickListener(){
              public void onClick(DialogInterface dialog, int id){
                   dialog.cancel();
                   System.exit(0);
              }
         });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
    }
}

www.un.org/spanish/ga/president AndersManifest.xml

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

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:icon="@drawable/sun_icon"
        android:label="@string/app_name" >
        <activity
            android:name="weather.right.nowActivity"
            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>

    <application
        android:icon="@drawable/sun_icon"
        android:label="@string/app_name" >
        <activity
            android:name="weather.right.About"
            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>

<>strong>EDIT: about.xml

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

    <TextView
        android:layout_width="252dp"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:text="@string/about" />

</LinearLayout>
最佳回答

参看<代码>的行文。 源自: j。 低于此线的权利:at climate.right.nowActative.onCreate (nowActative.java:37)

这意味着在<条码>上投下了<条码>。 如果你在这条线上重复点击的话,它将打开<条码>活性,并直接把你带往正出现错误的地方。 因此,牢记所有这一点。

我注意到的一件事是你重新使用<条码>。 onCreate 谨请在上使用<代码>about.xml。 而且,如果你认为被抛开的<条码>Exception<>/code>是由您的<条码>-Menu造成的,那么你就应当把该代码贴上与贵组织相关的位置。

参看<编码>37。 页: 1 保证您在<代码>Manifest上贴上标签,并确保您的<代码>Button是<代码>Layout的一部分,您在<代码>上重新使用<现成活度>。

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

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:icon="@drawable/sun_icon"
    android:label="@string/app_name" >
    <activity
        android:name="weather.right.nowActivity"
        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="weather.right.About"
        android:label="@string/app_name" />
</application>

</manifest>

不管怎样,<条码> 您首先要开始,或将出现在您的译员中,应当包括“<>Intent-Filter 贴出的标签,但其他。 活动不需要包括这些具体标签,有些可能需要<条码>。 另外,在<代码>AndroidManifest 上的所有内容都应在ONE><mani.4/1999/></mani.4/1999/> tag and >ONE/strong><application></application>

问题回答

这里看一下你的法典。 你们有2项要求提出最后意见,但这种意见却被搁置。 我猜测,该盒子正试图展示,但我只打电话,假设你真的希望是<条码>。

Now also I m going to guess your NullPointerException is because Button b = (Button) findViewById(R.id.menuItem1); is null. I think you created a menu by creating a menu XML file in the menu/ folder. What you re really looking for is a button in the layout/main.xml that has android:id="menuItem1". If that exists it ll find it, otherwise it ll return null. This code implies that it was created from a menu xml which if that s the case this is how you d handle that.

// Load the menu into the activity. Add this code to the activity class.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.example_menu, menu);
    return true;
}

// Handler for menu selections.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {

  case R.id.menuItem1: {
    // do work.
  } break;
  default:
    return super.onOptionsItemSelected(item);
  }

  return true;
}

尝试改变这一思路:

Intent i = new Intent(nowActivity.this, About.class);

Intent i = new Intent (getApplicationContext(), About.class);

并且,如果你使用碎片,就会:

Intent i = new Intent(getActivity().getApplicationContext(), About.class);

ok你正在使用观点。 因此,它看着这样的情况:

b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

and make sure you have the button id in your layout. Note: since the last setContentView you are using is: setContentView(R.layout.about);, the id of your button MUST be in that layout.





相关问题
In Eclipse, why doesn t "Show In" appear for non-Java files?

If I have a *java file open, I can right click on the source, scroll down to "Show In (Alt-Shift-W)", and select Navigator. If I have an *xml, *jsp, or pretty much anything besides a Java source ...

Eclipse: Hover broken in debug perspective

Since upgrading Eclipse (Galileo build 20090920-1017), hover in debug no longer displays a variable s value. Instead, hover behaves as if I were in normal Java perspective: alt text http://...

Eclipse smart quotes - like in Textmate

Happy Friday — Does anyone know if eclipse has the notion of smart quotes like Textmate. The way it works is to select some words and quote them by simply hitting the " key? I m a newbie here so be ...

Indentation guide for the eclipse editor

Is there a setting or plugin for eclipse that can show indentation guides in the editor? Something like the Codekana plugin for visual studio (not so fancy is also OK). Important that it works with ...

Adding jar from Eclipse plugin runtime tab

I want to add .jar files for plugin from the Runtime tab of manifest file. When I use the Add... button, I can see only sub-directories of the plugin project. So if I want to add same .jar file to ...

How to copy multiple projects into single folder in eclipse

I have two projects, Project1 and Project2. Now i want to copy this projects into eclipse workspace but i want these projects to be in a single folder like below. Eclipse Workspace -> Project -> ...

Java: Finding out what is using all the memory

I have a java application that runs out of memory, but I have no idea which code is allocating the memory. Is there an application with which I can check this? I use Eclipse.