2015-10-16 26 views
8

Tôi mới phát triển Android và có một chút vấn đề về thay đổi hoạt động. Tôi đang cố gắng thay đổi các hoạt động từ bên trong một phương pháp nhưng tôi gặp lỗi cannot resolve method startActivity và trên tham số kết thúc lỗi Cannot resolve constructor 'Intent (...)'. Tôi tìm thấy a question here với cùng một loại vấn đề và cố gắng thực hiện các câu trả lời của họ vào chương trình của tôi nhưng không có niềm vui.Không thể giải quyết phương thức startActivity()

Dưới đây là các mã:

public void open301(View view) { 
    startActivity(new Intent(CustomAdapter.this, ThreeZeroOne.class)); 
} 

trước khi nhìn vào các câu trả lời từ câu hỏi liên kết ở trên mã trông như thế này với các lỗi tương tự:

public void open301(View view) { 
    Intent openThree = new Intent(this,ThreeZeroOne.class); 
    startActivity(openThree); 
} 

Toàn bộ mã:

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.content.Intent; 

public class CustomAdapter extends BaseAdapter { 

String[] result; 
Context context; 
int[] imageId; 

private static LayoutInflater inflater = null; 

public CustomAdapter(selectGame SelectGame, String[] prgmNameList, int[] prgmImages) { 
    result = prgmNameList; 
    context = SelectGame; 
    this.imageId = prgmImages; 
    inflater = (LayoutInflater) context.getSystemService(Context. 
      LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 
    return result.length; 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

public class Holder { 
    TextView tv; 
    ImageView img; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    Holder holder = new Holder(); 
    View rowView; 
    rowView = inflater.inflate(R.layout.game_selection, null); 
    holder.tv = (TextView) rowView.findViewById(R.id.txt); 
    holder.img = (ImageView) rowView.findViewById(R.id.img); 
    holder.tv.setText(result[position]); 
    holder.img.setImageResource(imageId[position]); 
    rowView.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(context, "Beginning game " + result[position], Toast.LENGTH_SHORT).show(); 

     } 
    }); 
    return rowView; 
} 

public void open301(View view) { 
    Intent openThree = new Intent(this,ThreeZeroOne.class); 
    startActivity(openThree); 
} 

}

+0

Bạn thử thực hiện phương pháp ở đâu? trong một Acticity? Lớp học? Miếng? –

+0

Trong một lớp học, tôi sẽ đăng mã nơi nó nằm. – COYG

Trả lời

26

Bạn nên sử dụng bối cảnh của bộ chuyển đổi của bạn:

public void open301(View view) { 
    Intent openThree = new Intent(context,ThreeZeroOne.class); 
    context.startActivity(openThree); 
} 
+2

'Intent (this,' là sai – Blackbelt

2

Để bắt đầu một hoạt động mới, bạn sẽ cần một bối cảnh bắt đầu từ đâu, và hoạt động hiện tại của bạn "BaseAdapter" không phải là một bối cảnh, luckly mỗi điểm có một bối cảnh, vì vậy bạn có thể làm như thế này:

public void open301(View view) { 
    Intent openThree = new Intent(view.getContext(), ThreeZeroOne.class); 
    view.getContext().startActivity(openThree); 
} 
+0

Điều đó làm việc một chút, tôi sẽ không cần phải đặt tên ý định trong dấu ngoặc đơn của startActivity? bằng không làm thế nào nó sẽ biết hoạt động nào để bắt đầu. - điều này liên quan đến bình luận cũ của bạn. – COYG

+0

Tôi chỉnh sửa câu trả lời của mình để làm rõ, bất kỳ nghi ngờ nào khác? –

+1

'Intent (điều này,' là sai – Blackbelt

1

Đầu tiên lấy bối cảnh của bạn:

private Context context; 

public CustomAdapter(Context context) { 
     this.context = context;  
} 

Và sau đó:

  context.startActivity(openThree); 
Các vấn đề liên quan