片段中的意图不断与清单崩溃问题,android studio
Posted
技术标签:
【中文标题】片段中的意图不断与清单崩溃问题,android studio【英文标题】:intent in fragment keeps crashing problem with the manifest, android studio 【发布时间】:2019-10-25 08:43:15 【问题描述】:我收到了错误
android.content.ActivityNotFoundException:无法找到显式活动类 com.example.tulio.appkolev/com.example.tulio.appkolev.fragments.registerFragment;您是否在 AndroidManifest.xml 中声明了此活动?
在网上看了几页后,我找不到解决办法
基础是,我有这个布局可以在页面末尾登录应用程序我有一个创建帐户的链接。单击时,应用程序崩溃并显示该错误
我确定代码不符合标准
主要
package com.example.tulio.appkolev;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
private final AppCompatActivity activity = MainActivity.this;
private DrawerLayout drawer;
private InputValidation inputValidation;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar,
R.string.navigation_drawer_close, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState==null)
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new loginFragment()).commit();
navigationView.setCheckedItem(R.id.account);
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
switch (menuItem.getItemId())
case R.id.settings:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new settingsFragment()).commit();
break;
case R.id.account:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new loginFragment()).commit();
break;
case R.id.assignments:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new assigmentsFragment()).commit();
break;
drawer.closeDrawer(GravityCompat.START);
return true;
@Override
public void onBackPressed()
if (drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);
else
super.onBackPressed();
private void initObjects()
databaseHelper = new DatabaseHelper(activity);
inputValidation = new InputValidation(activity);
登录类
package com.example.tulio.appkolev.fragments;
import com.example.tulio.appkolev.R;
import com.example.tulio.appkolev.helper.InputValidation;
import com.example.tulio.appkolev.sql.DatabaseHelper;
public class loginFragment extends Fragment implements View.OnClickListener
private final Fragment fragment = loginFragment.this;
private NestedScrollView nestedScrollView;
private TextInputLayout input_email;
private TextInputLayout input_password;
private TextInputEditText input_edit_email;
private TextInputEditText input_edit_password;
private AppCompatButton button_login;
private AppCompatTextView registerLink;
private InputValidation inputValidation;
private DatabaseHelper databaseHelper;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_login, container, false);
// initObjects();
button_login = (AppCompatButton) v.findViewById(R.id.button_login);
button_login.setOnClickListener(this);
registerLink = (AppCompatTextView) v.findViewById(R.id.registerLink);
registerLink.setOnClickListener(this);
nestedScrollView = (NestedScrollView) v.findViewById(R.id.nestedScrollView);
input_email = (TextInputLayout)v.findViewById(R.id.input_email);
input_password = (TextInputLayout)v.findViewById(R.id.input_password);
input_edit_email = (TextInputEditText) v.findViewById(R.id.input_edit_email);
input_edit_password = (TextInputEditText) v.findViewById(R.id.input_edit_password);
button_login = (AppCompatButton) v.findViewById(R.id.button_login);
registerLink = (AppCompatTextView) v.findViewById(R.id.registerLink);
button_login.setOnClickListener(this);
registerLink.setOnClickListener(this);
return v;
@Override
public void onClick(View v)
switch (v.getId())
case R.id.button_login:
verifyFromSQLite();
break;
case R.id.registerLink:
Intent intentRegister = new Intent(getActivity().getApplicationContext(), registerFragment.class);
startActivity(intentRegister);
break;
/* private void initObjects()
databaseHelper = new DatabaseHelper(this);
inputValidation = new InputValidation();
*/
private void verifyFromSQLite()
if (!inputValidation.isInputEditTextFilled(input_edit_email, input_email, getString(R.string.error_message_email)))
return;
if (!inputValidation.isInputEditTextEmail(input_edit_email, input_email, getString(R.string.error_message_email)))
return;
if (!inputValidation.isInputEditTextFilled(input_edit_password, input_password, getString(R.string.error_message_email)))
return;
if (databaseHelper.checkUser(input_edit_email.getText().toString().trim()
, input_edit_password.getText().toString().trim()))
Intent accountsIntent = new Intent(getActivity().getApplication(), profileFragment.class);
accountsIntent.putExtra("EMAIL", input_edit_email.getText().toString().trim());
emptyInputEditText();
startActivity(accountsIntent);
else
// Snackbar.make(nestedScrollView, getString(R.string.error_valid_email_password), Snackbar.LENGTH_LONG).show();
private void emptyInputEditText()
input_edit_email.setText(null);
input_edit_password.setText(null);
注册类
package com.example.tulio.appkolev.fragments;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.tulio.appkolev.R;
public class registerFragment extends Fragment
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return inflater.inflate(R.layout.fragment_register, container, false);
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tulio.appkolev">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我想在点击时将我带到收银台布局。请告诉我所有我可以改进的地方
【问题讨论】:
【参考方案1】:Intent intentRegister = new Intent(getActivity().getApplicationContext(),registerFragment.class);
startActivity(intentRegister);
registerFragment 是一个 Fragment,而不是活动。 你不能通过 startActivity 来启动它。 您可以更改 registerFragmentis 扩展活动并在 AndroidManifest.xml 中声明此活动
【讨论】:
【参考方案2】:下一行导致问题。
Intent intentRegister = new Intent(getActivity().getApplicationContext(),registerFragment.class);
startActivity(intentRegister);
Intent 仅用于活动,而您使用该意图调用 Fragment 显然会给您带来崩溃。因此,您必须为此使用片段管理器,而不是使用意图调用片段。
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new registerFragment())
.commit();
注意:对于您在项目中使用的任何类,最好使用驼峰命名标准。
【讨论】:
以上是关于片段中的意图不断与清单崩溃问题,android studio的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin Android C#:使用 xml 资源文件夹和清单来定义用于检测连接的 USB 设备崩溃应用程序的意图过滤器