2014-04-16 5 views
0

안녕하세요 저는 Android에서 응용 프로그램을 만들기 시작했습니다.이 프로그램은 간단한 BMI 계산기입니다. 응용 프로그램에는 2 개의 활동이 있습니다 (메뉴가있는 Main과 위젯이있는 BMICalculator).OnClickListner - android Onclick가 작동하지 않습니다.

제 문제는 OnClickListener 내 응용 프로그램이 충돌하여 Main에서 BMIcalculator로 전달하려고 할 때 발생하는 문제입니다. 나는 또한 android : OnClick에 의해 그것을 시도하고이 방법으로, 나는 활동을 할 수 있지만, "oblicz"버튼 (BMI 계산 용)을 클릭하면 응용 프로그램이 충돌합니다. /이 제발 도와주세요 : 나는 많은 것들을 시도 웹을 검색하지만 어떤 솔루션을 찾을 수 없습니다

전에서 gettext를하지 않으면

/\ OnClickListener를 일 : 여기서 D 는 코드입니다()

MainActivity.java

package com.example.labswm; 

public class MainActivity extends ActionBarActivity { 

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

@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 gotoP1(View view){ 
    Intent intent = new Intent(this, Page1Activity.class); 
    startActivity(intent); 


} 

}

mainXML

01,235,
<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.labswm.MainActivity$PlaceholderFragment" > 

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

<Button 
    android:id="@+id/gotoP1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/opis" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="132dp" 
    android:layout_marginLeft="40dp" 
    android:onClick="gotoP1" 
    android:text="@string/oblicz_ibm" /> 

    <requestFocus /> 
</EditText> 

BMIACtivity (명명 Page1Activity)

package com.example.labswm; 



public class Page1Activity extends ActionBarActivity { 

public Button oblicz; 
public EditText waga; 
public EditText wzrost; 
public RadioGroup group; 
public TextView result; 

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

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



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.page1, 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_page1, 
       container, false); 
     return rootView; 
    } 
} 
public void calculateClick(View view){ 
    if(view.getId()==R.id.oblicz){ 

//THE ERROR STARTS HERE ! 
     EditText wag = (EditText)findViewById(R.id.waga); 
     EditText wzr = (EditText)findViewById(R.id.wzrost); 
     TextView res = (TextView)findViewById(R.id.result); 

     float fwaga = Float.parseFloat(wag.getText().toString()); 
     float fwzrost = Float.parseFloat(wzrost.getText().toString()); 
     float rez = (float) (fwaga * 4.88/fwzrost * fwzrost); 
     res.setText("BMI" + rez); 

    } 
} 





} 

Page1Activity.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" 
android:launchMode = "singleInstance" 
tools:context="com.example.labswm.Page1Activity$PlaceholderFragment" > 

<TextView 
    android:id="@+id/opis" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="@string/opis_to_okno_pozwla_na_obliczenie_swojego_ibm_" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/opis" 
    android:layout_below="@+id/opis" 
    android:layout_marginTop="32dp" 
    android:text="@string/waga_" /> 

<EditText 
    android:id="@+id/waga" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:ems="10" 
    android:inputType="numberDecimal" > 

    <requestFocus /> 
</EditText> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/waga" 
    android:layout_below="@+id/waga" 
    android:text="@string/wzrost_" /> 

<EditText 
    android:id="@+id/wzrost" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView2" 
    android:layout_below="@+id/textView2" 
    android:ems="10" 
    android:inputType="numberDecimal" /> 

<Button 
    android:id="@+id/oblicz" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/group" 
    android:layout_below="@+id/group" 
    android:onClick="calculateClick" 
    android:text="@string/oblicz_" /> 

<RadioGroup 
    android:id="@+id/group" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/wzrost" 
    android:layout_below="@+id/wzrost" 
    android:layout_marginTop="42dp" > 

    <RadioButton 
     android:id="@+id/rb1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/centymetry" /> 

    <RadioButton 
     android:id="@+id/rb2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/metry" 
     tools:ignore="ObsoleteLayoutParam" /> 
</RadioGroup> 

<TextView 
    android:id="@+id/result" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/oblicz" 
    android:layout_marginTop="31dp" 
    android:layout_toRightOf="@+id/textView2" 
    android:textSize="20sp" /> 

LOG

04-16 21:31:31.063: D/dalvikvm(12362): GC_EXTERNAL_ALLOC freed 87K, 47% free 2855K/5379K, external 0K/0K, paused 77ms 
04-16 21:31:31.503: D/CLIPBOARD(12362): Hide Clipboard dialog at Starting input: finished by someone else... ! 
04-16 21:31:33.153: D/dalvikvm(12362): GC_CONCURRENT freed 121K, 46% free 3076K/5639K, external 328K/1281K, paused 2ms+3ms 
04-16 21:31:38.093: W/dalvikvm(12362): threadid=1: thread exiting with uncaught exception (group=0x4001e578) 
04-16 21:31:38.103: E/AndroidRuntime(12362): FATAL EXCEPTION: main 
04-16 21:31:38.103: E/AndroidRuntime(12362): java.lang.IllegalStateException: Could not execute method of the activity 
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.view.View$1.onClick(View.java:2185) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.view.View.performClick(View.java:2585) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.view.View$PerformClick.run(View.java:9299) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.os.Handler.handleCallback(Handler.java:587) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.os.Looper.loop(Looper.java:130) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.app.ActivityThread.main(ActivityThread.java:3691) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at java.lang.reflect.Method.invokeNative(Native Method) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at java.lang.reflect.Method.invoke(Method.java:507) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at dalvik.system.NativeStart.main(Native Method) 
04-16 21:31:38.103: E/AndroidRuntime(12362): Caused by: java.lang.reflect.InvocationTargetException 
04-16 21:31:38.103: E/AndroidRuntime(12362): at java.lang.reflect.Method.invokeNative(Native Method) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at java.lang.reflect.Method.invoke(Method.java:507) 
04-16 21:31:38.103: E/AndroidRuntime(12362): at android.view.View$1.onClick(View.java:2180) 
04-16 21:31:38.103: E/AndroidRuntime(12362): ... 11 more 
04-16 21:31:38.103: E/AndroidRuntime(12362): Caused by: java.lang.NullPointerException 
04-16 21:31:38.103: E/AndroidRuntime(12362): at com.example.labswm.Page1Activity.calculateClick(Page1Activity.java:89) 
04-16 21:31:38.103: E/AndroidRuntime(12362): ... 14 more 
04-16 21:31:40.113: I/dalvikvm(12362): threadid=4: reacting to signal 3 
04-16 21:31:40.113: I/dalvikvm(12362): Wrote stack traces to '/data/anr/traces.txt' 
01

public EditText waga; 
public EditText wzrost; 

을하지만 당신은 그들을 초기화하지 : 23,516,

+1

excepion 코드를 추가하십시오 –

+0

[편집] 버튼을 클릭하고 LogCat에서 오류가 발생한 위치를 나타내는 행을 추가하십시오.버튼 클릭 리스너가 정상적으로 보이는 것처럼 이것이 여러 액티비티와 매니페스트의 오류임을 알 수 있습니다. –

+1

'원인 : java.lang.NullPointerException 04-16 21 : 31 : 38.103 : E/AndroidRuntime (12362) : com.example.labswm.Page1Activity.calculateClick (Page1Activity.java:89)'무엇이 89 행입니까? – Simon

답변

0

를 정의해야합니다, 당신은 당신의 EditText 변수를 선언합니다.

당신은 그러므로 .getText()는 예외가 발생, waga = (EditText)findViewById(R.id.waga);

당신이 특정 오류가 wzrost가 null 있음을 의미 라인 89에서 널 포인터 같은 것을해야합니다. 이 라인에서

+0

네, 그렇지만 사용하지는 않습니다. calculateClick에서 나는 wag와 wzr을 초기화했다. 그리고 초기화 된 waga와 wzrost도 시도해 보았습니다 :/ – sincrow

+0

네, 사용중입니다. - float fwzrost = Float.parseFloat (wzrost.getText(). toString());'어디에서'wzrost'를 초기화합니까? – Simon

+0

좋아 wzrst를 wzr로 바꿨다. 고마워요. 그렇지만 아직도 공개 된 EditText waga = null; 및 wzrost = null; 그리고 나는 waga = (EditText) findViewById (R.id.waga)를하고 있었다. OnCreate() 및 waga.setOnClickListener (calculateListener)를 수행 한 후; 나는 나의 활동 1에서 BMI와 함께 행동주의로 넘어갈 수 없었다; p – sincrow

-1
Button b1=(Button)findViewById(R.id.gotoP1); 
b1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) {} 
     }); 

당신의 XML을 확인하지만 MainActivity에서 해당 버튼을 여기에

+0

그것은 onClick 리스너를 설정하는 또 다른 방법이지만 xml에서이 리스너를 설정하는 데 문제가 있습니다. –

0

봐 : 당신은 wzrost를 사용하지만, 당신이 그것을 초기화되지 않았습니다

float fwzrost = Float.parseFloat(wzrost.getText().toString()); 

. 방금 초기화했습니다. wzr ==> NPE

0

초기화되지 않은 변수 wzrostfloat fwzrost = Float.parseFloat(wzrost.getText().toString());에 사용하고 있습니다. 나는 당신이 그것이 wzr 인 것을 의미했다고 생각하고 있습니다.

관련 문제