2016-05-25 17 views
0

Die App verknallt sich, wenn ich versuche, eine Ganzzahl von einer Aktivität an eine andere zu übergeben. alles funktioniert gut. die Methoden und starten eine neue Aktivität. hier ist mein Code:Wie eine ganze Zahl zwischen Aktivitäten übergeben?

import android.content.Intent; 
import android.os.Bundle; 


public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    if (android.os.Build.VERSION.SDK_INT > 9) { 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
    } 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.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(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 


public int URLi() { 
. 
./code 
. 
. 
. 
    return celsius; 
} 

public void onclick (View v) { 
    int temp=URLi(v); 
    Intent intent = new Intent(this, TheTemp.class); 
    intent.putExtra("temp",temp); 
    startActivity(intent); 
} 

}

die neue Aktivität

public class TheTemp extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_the_temp); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
} 

Intent intent = getIntent(); 
int receiveValue = getIntent().getIntExtra("temp",0); 

}

manifest

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 

      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".TheTemp" 
     android:label="@string/title_activity_the_temp" 
     android:theme="@style/AppTheme.NoActionBar"></activity> 
</application> 

und die Fehler, die ich erhalten:

05-25 16:05:01.047 11632-11632/com.example.owner.MYproject E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.example.owner.MYproject, PID: 11632 
                      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.Owner.MYproject/com.example.Owner.Myproject.TheTemp}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2439) 
                       at android.app.ActivityThread.access$800(ActivityThread.java:162) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:135) 
                       at android.app.ActivityThread.main(ActivityThread.java:5421) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:372) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) 

danken für Ihre Hilfe!

Antwort

0

Holen Sie den Wert innerhalb Ihrer onCreate() Methode. Derzeit holen Sie es außerhalb der Methode ab.

public class TheTemp extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_the_temp); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    Intent intent = getIntent(); 
    int receiveValue = getIntent().getIntExtra("temp",0); 
} 
} 
+0

im kinda eines Noob bei Android, so dass Sie mir wirklich geholfen, aus Vielen Dank. BTW, gibt es eine Anleitung zum Zeichnen eines Int in Canvas? – segev

+0

Glücklich zu helfen ... :) Akzeptieren Sie die Antwort, so dass es für die zukünftigen Besucher hilfreich sein wird. –

+0

Durch Klicken auf das grüne V rechts? – segev

Verwandte Themen