2014-11-23 59 views
0

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/由于onClickListener造成的崩溃

初学者的android dev在这里。 我目前正在按照本指南尝试创建一个返回用户位置的Android应用程序。

package com.example.newapp; 



import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.ActionBarActivity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.Toast; 



public class MainActivity extends ActionBarActivity { 

    Button btnShowLocation; 

    GPSTracker gps; 

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

     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()) 
        .commit(); 
     } 

     btnShowLocation = (Button) findViewById(R.id.btnShowLocation); 

     //show location button click event 
     btnShowLocation.setOnClickListener(new View.OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       //create class object 
       gps = new GPSTracker(MainActivity.this); 

       //check if GPS enabled 
       if (gps.canGetLocation()){ 

        double latitude = gps.getLatitude(); 
        double longitude = gps.getLongitude(); 

        // \n is for new line 
        Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
       } else{ 
        //can't get location 
        gps.showSettingsAlert(); 
       } 
      } 
     }); 
    } 


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

    @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(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      return rootView; 
     } 
    } 

    public void sendLocation(View view){ 
     AlertDialog.Builder builder1 = new AlertDialog.Builder(this); 
     builder1.setMessage("Your latitude is " + gps.getLatitude() + " and your longitude is " + gps.getLongitude()); 
     builder1.setCancelable(true); 
     AlertDialog alert1 = builder1.create(); 
     alert1.show(); 
    } 

} 

我fragment_main.xml是像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.newapp.MainActivity$PlaceholderFragment" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <EditText android:id="@+id/edit_message" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_message" /> 

    <Button 
     android:id="@+id/btnShowLocation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="118dp" 
     android:text="Button" 
     android:onClick="sendLocation" /> 

</RelativeLayout> 

我注释掉onClickListener的东西,我的应用程序正常工作,所以我知道这个问题是这一点。我只是不确定它会在启动时崩溃我的应用程序。

回答

0

呀,这是片段一件棘手的事情里面写。向活动添加片段时,不会立即添加片段。您正在添加片段,然后立即尝试查找片段内的按钮。不幸的是,该按钮不在您的视图层次结构中。因此,您的btnShowLocation为null,并且当您尝试在按钮上设置侦听器时,会在启动时发生崩溃。

你需要做的是配置你的片段代码中的按钮,而不是你的活动代码。无论如何,这是完成它的合理位置,因为按钮位于片段的布局中。

试试这个:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

    // Notice that you have to call findViewById on rootView, NOT the fragment. 
    // This is because you are in the process of telling Android about your fragment's 
    // views, but you haven't actually returned the view hierarchy yet 
    Button btnShowLocation = (Button) rootView.findViewById(R.id.btnShowLocation); 

    //show location button click event 
    btnShowLocation.setOnClickListener(new View.OnClickListener() { 
     ... 
    } 
    return rootView; 
} 

您还可以从你的活动向下移动您的GPS领域的片段。当你使用片段时,你的活动变得非常简单,并且大部分逻辑可以存在于片段中。

+0

我跟着这个,现在我的应用程序启动,但当我按下按钮时它崩溃。 我把onClickListener放在“gps = new GPSTracker(gps);”以及Toast.makeTest(gps,...);因为其他参数不适用于片段中的代码。 – Divide0 2014-11-23 07:45:37

+0

没关系,我通过使用getActivity()来取得它的功能。非常感谢你的帮助! – Divide0 2014-11-23 08:04:28

0

你都用了

  setOnClickListener in class 

  android:onClick="sendLocation" in your xml 

使用的任何一个。

0

删除该行:

android:onClick="sendLocation" 

从你的XML,按钮标签