最初我收到错误"这个片段内部类应该是静态的"在以下代码中:
public class Activity4 extends FragmentActivity {
EditText mEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity4);
}
public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
public void populateSetDate(int year, int month, int day) {
mEdit = (EditText) findViewById(R.id.editText);
mEdit.setText(day + "/" + month + "/" + year);
}
//error was here at SelectDateFragment
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm + 1, dd);
}
}
}
我试图找出解决方案&在这里阅读一些帖子,它是一个lint错误,可以通过将它添加到gradle文件来解决:
lintOptions {
abortOnError false
}
但是当我这样做时,我得到的信息是"无法解析符号getDefaultProguardFile"。这是我的gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.sample.pat1"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
}
以前的错误"这个片段内部类应该是静态的"仍然坚持。令人惊讶的是,尽管如此,该应用程序仍能正常运行..但我想知道我哪里出错......请帮忙!
答案 0 :(得分:0)
如果您不想重写通讯机制,可以手动将片段标记为有效:
@SuppressLint("ValidFragment")
DialogFragment newFragment = new SelectDateFragment();
更多说明here
P.S。 Proguard - 这是另一个问题。