2014-03-13 3 views
0

제대로 작동하려면 onClick 코딩 문제가 발생합니다. 8 개의 버튼이 포함 된 주 활동이 있습니다. 버튼 1은 2,4,5,6 및 7 페이지의 페이스 북을 열어야합니다. 정의 된 페이지로 웹 브라우저를 열고 8 번 버튼을 눌러 전화 걸기를 시작하여 미리 설정된 번호로 전화를 겁니다. 나는이 응용 프로그램을 충돌과 함께 작동하도록 얻을 수 없다 버튼 1에 하나의 onclick 청취자를 설정할 수 있고 작동하도록하지만, 다음에 하나 추가 할 때 응용 프로그램이 충돌 나는 일식에서 아무런 오류가 발생하지 않고 함께 할 onclicks를 처리 할 조각하지만 메신저 어떻게 이런 일을 잃어버린 어느 누구도이 코딩을 도와 줄 수 있습니까 ???onClick Listener 오류가 발생하지 않음

이것은 내가 지금까지 2 개의 버튼을 위해 아직도 얼굴 전화 번호부를 여는 사람이 없거나 전화 다이얼러를 열어 본 사람이다. 그러나 이것은 내가 충돌 할 때까지이다. - Jerry 33 분전

import android.net.Uri; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

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

    public void addButtonClickListner() { 
     Button btnNavigator = (Button)findViewById(R.id.imageButton2);     
     btnNavigator.setOnClickListener(new OnClickListener(){ 
      public void onClick(View arg) { 
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("www.*****.com")); 
        startActivity(intent); 
      } 
     }); 
    } 
+6

코드를 게시하면서 마음에 드는 사람이 쉽게이 문제를 해결할 수 있다고 확신합니다. – zgc7009

+0

이것은 내가 지금까지 가지고있는 2 개의 버튼에 대해서 아직도 페이스 북을 열지 않았거나 전화 걸기를 열지 못했지만 이것은 내가 충돌 할 때까지이다. – Jerry

+0

import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; 가져 오기 android.view.View.OnClickListener; 가져 오기 android.widget.Button; public class MainActivity extends Activity { – Jerry

답변

0

대신

....

package com.example.testcode; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
// Test code: import android.widget.TextView; 

// Let your class implement the OnClickListener interface directly. This 
// will let you use the onClickListener 
class MainActivity extends Activity implements OnClickListener{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // "find" our views by their id's in our activity's layout 
     Button button1 = (Button) findViewById(R.id.button1); 
     Button button2 = (Button) findViewById(R.id.button2); 
     //...... continue for button3 - button8 

     // set our "click" listeners for each of our buttons 
     button1.setOnClickListener(this); 
     button2.setOnClickListener(this); 
     //...... continue for button3 - button 8 
    } 

    // Because our class implements the OnClickListener interface 
    // it will be listening for "clicks". Because of this, we can 
    // override the click listener's default onClick(View v) method. 
    // View v is our view, or our button, that is "clicked". 
    @Override 
    public void onClick(View v) { 
     // Test code: TextView text = (TextView) findViewById(R.id.text); 

     // This is the statement that will allow each of your buttons 
     // to perform different processes. For my test code, I have each 
     // button reset the TextView I have displayed in the top of my 
     // layout. 
     switch(v.getId()){ 
     case R.id.button1: 
      // Test code: text.setText("Button 1"); 
      break; 
     case R.id.button2: 
      // Test code: text.setText("Button 2"); 
      break; 
     //...... continue for button3 - button 8 
     default: 
      Log.d(getApplication().getPackageName(), "Button click error!"); 
      break; 
     } 
    } 
} 

내 의견에서 봐 주시기 바랍니다이 작업을 수행합니다.

// Test Code: 

으로 주석 처리 된 부분은 수행중인 작업에 대해 거의 쓸모없는 코드입니다. switch 문에서 테스트 코드는 각 개별 단추로 수행 할 작업으로 바꿔야합니다. "// ...... button3-button8에 대해 계속하라"는 말은 나머지 6 개가있는 처음 두 개의 버튼으로 시작한 패턴을 반복한다는 의미입니다. 테스트 코드 라인이 더 이상 주석되도록 : 여기 내 레이아웃입니다, 내 주어진 테스트 코드와 테스트하려는 경우

(당신이 삭제 "// 테스트 코드를"확인되지

<LinearLayout 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:orientation="vertical" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Press a button" 
     android:id="@+id/text" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 1" 
     android:id="@+id/button1" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 2" 
     android:id="@+id/button2" /> 

</LinearLayout> 

참고 :

"좋은"XML 레이아웃의에서 이해하는 것이 중요하다 몇 가지 일반적인 대신 : '텍스트 = @ 문자열/"string_id"안드로이드 "안드로이드 텍스트 ="부분으로 설정됩니다 현지화에 도움이되는 문자열입니다.

해당 최상위 줄을 dec 이 패키지의 이름은 "Application"/ src/"package_name"에 있으며 대개 com.example.applicationname과 같은 것입니다.

나는 또한 다음과 같이 말하고 싶습니다. 코드에 주석을 포함 시켰습니다.이 링크를 반복적으로 다시 방문하지 않아도 복사 및 붙여 넣기가 가능하므로 누구나 코드를 지침으로 사용하려는 경우가 아니라 답변에 게시 된 코드가 많기 때문입니다.

+0

그냥이 방법을 따르십시오 ... 이해하는 것이 가장 쉽고 ... 그리고 그가 구현하고있는 수업을 기록해 두십시오. – 1baga

+0

THank 여러분, 오늘이 기회를 놓치지 마시고 댓글을 다시 작성해 드리겠습니다. – Jerry

+0

Of 물론, 만약 작품 upvote 받아 accept ... 나는 점을 좋아해 : P – zgc7009

관련 문제